from django.http import HttpResponse from django.db.models import Q, CharField import json from cStringIO import StringIO def datatables_view(request): objects = MyModel.objects.all() list_display = ['field1', 'field2', ...] list_filter = [f.name for f in MyModel._meta.fields if isinstance(f, CharField)] #a simple way to bring all CharFields, can be defined in specifics # count total items: iTotalRecords = objects.count() #filter on list_filter using __contains search = request.GET['sSearch'] queries = [Q(**{f+'__contains' : search}) for f in list_filter] qs = reduce(lambda x, y: x|y, queries) objects = objects.filter(qs) #sorting order = dict( enumerate(list_display) ) dirs = {'asc': '', 'desc': '-'} ordering = dirs[request.GET['sSortDir_0']] + order[int(request.GET['iSortCol_0'])] objects = objects.order_by(order_by) # count items after filtering: iTotalDisplayRecords = objects.count() # finally, slice according to length sent by dataTables: start = int(request.GET['iDisplayStart']) length = int(request.GET['iDisplayLength']) objects = objects[ start : (start+length)] # extract information data = [map(lambda field: getattr(obj, field), list_display) for obj in objects] #define response response = { 'aaData': data, 'iTotalRecords': iTotalRecords, 'iTotalDisplayRecords': iTotalDisplayRecords, 'sEcho': request.GET['sEcho'] } #serialize to json s = StringIO() json.dump(response, s) s.seek(0) return HttpResponse(s.read())