Package: dj-datatables-view
AboutFork from django-datatables-view
Compatible with Django 2+ 3+ 4+dj-datatables-view is a base view for handling server side
processing for the awesome datatables 1.9.*, 1.10.*, 1.11.*http://datatables.netdj-datatables-view simplifies handling of sorting, filtering and
creating JSON output, as defined at:https://datatables.net/examples/server_sideUsage1. Install dj-datatables-viewpip install dj-datatables-view2. Edit views.pydjango_datatables_viewusesGenericViews, so your view should
just inherit from base class:BaseDatatableView, and override few
things (there is also a DatatableMixin - pure datatables handler that
can be used with the mixins of your choice, eg. django-braces). These
are:model- the model that should be used to populate the datatablecolumns- the columns that are going to be displayed. If not
defined then django_datatables_view will look for ‘name’ in the
columns definition provided in the request by DataTables, eg.:
columnDefs: [{name: ‘name’, targets: [0]} (only works for datatables
1.10+)order_columns- list of column names used for sorting (eg. if
user sorts by second column then second column name from this list
will be used with order by clause). If not defined then
django_datatables_view will look for ‘name’ in the columns
definition provided in the request by DataTables, eg.: columnDefs:
[{name: ‘name’, targets: [0]} (only works for datatables 1.10+)filter_queryset- if you want to filter your DataTable in some
specific way then override this method. In case of older DataTables
(pre 1.10) you need to override this method or there will be no
filtering.filter_method- returns ‘istartswith’ by default, you can
override it to use different filtering method, e.g. icontains: return
self.FILTER_ICONTAINSFor more advanced customisation you might want to override:get_initial_queryset- method that should return queryset used
to populate datatableprepare_results- this method should return list of lists (rows
with columns) as needed by datatablesescape_values- you can set this attribute to False in order to
not escape values returned from render_column methodThe code is rather simple so do not hesitate to have a look at it.
Method that is executed first (and that calls other methods to execute
whole logic) isget_context_data. Definitely have a look at this
method!See example below:fromdj-datatables-view.base_datatable_viewimportBaseDatatableViewfromdjango.utils.htmlimportescapeclassOrderListJson(BaseDatatableView):# The model we're going to showmodel=MyModel# define the columns that will be returnedcolumns=['number','user','state','created','modified']# define column names that will be used in sorting# order is important and should be same as order of columns# displayed by datatables. For non sortable columns use empty# value like ''order_columns=['number','user','state','','']# set max limit of records returned, this is used to protect our site if someone tries to attack our site# and make it return huge amount of datamax_display_length=500defrender_column(self,row,column):# We want to render user as a custom columnifcolumn=='user':# escape HTML for security reasonsreturnescape('{0}{1}'.format(row.customer_firstname,row.customer_lastname))else:returnsuper(OrderListJson,self).render_column(row,column)deffilter_queryset(self,qs):# use parameters passed in GET request to filter queryset# simple example:search=self.request.GET.get('search[value]',None)ifsearch:qs=qs.filter(name__istartswith=search)# more advanced example using extra parametersfilter_customer=self.request.GET.get('customer',None)iffilter_customer:customer_parts=filter_customer.split(' ')qs_params=Noneforpartincustomer_parts:q=Q(customer_firstname__istartswith=part)|Q(customer_lastname__istartswith=part)qs_params=qs_params|qifqs_paramselseqqs=qs.filter(qs_params)returnqs3. Edit urls.pyAdd typical django’s urlconf entry:url(r'^my/datatable/data/$',login_required(OrderListJson.as_view()),name='order_list_json'),4. Define HTML + JavaScriptExample JS:$(document).ready(function(){varoTable=$('.datatable').dataTable({// ..."processing":true,"serverSide":true,"ajax":"{% url 'order_list_json' %}"});// ...});Another example of views.py customisationfromdj-datatables-view.base_datatable_viewimportBaseDatatableViewfromdjango.utils.htmlimportescapeclassOrderListJson(BaseDatatableView):order_columns=['number','user','state']defget_initial_queryset(self):# return queryset used as base for futher sorting/filtering# these are simply objects displayed in datatable# You should not filter data returned here by any filter values entered by user. This is because# we need some base queryset to count total number of records.returnMyModel.objects.filter(something=self.kwargs['something'])deffilter_queryset(self,qs):# use request parameters to filter queryset# simple example:search=self.request.GET.get('search[value]',None)ifsearch:qs=qs.filter(name__istartswith=search)# more advanced examplefilter_customer=self.request.GET.get('customer',None)iffilter_customer:customer_parts=filter_customer.split(' ')qs_params=Noneforpartincustomer_parts:q=Q(customer_firstname__istartswith=part)|Q(customer_lastname__istartswith=part)qs_params=qs_params|qifqs_paramselseqqs=qs.filter(qs_params)returnqsdefprepare_results(self,qs):# prepare list with output column data# queryset is already paginated herejson_data=[]foriteminqs:json_data.append([escape(item.number),# escape HTML for security reasonsescape("{0}{1}".format(item.customer_firstname,item.customer_lastname)),# escape HTML for security reasonsitem.get_state_display(),item.created.strftime("%Y-%m-%d%H:%M:%S"),item.modified.strftime("%Y-%m-%d%H:%M:%S")])returnjson_dataYet another example of views.py customisationThis sample assumes that list of columns and order columns is defined on
the client side (DataTables), eg.:$(document).ready(function(){vardt_table=$('.datatable').dataTable({order:[[0,"desc"]],columnDefs:[{name:'name',orderable:true,searchable:true,targets:[0]},{name:'description',orderable:true,searchable:true,targets:[1]}],searching:true,processing:true,serverSide:true,stateSave:true,ajax:TESTMODEL_LIST_JSON_URL});});classTestModelListJson(BaseDatatableView):model=TestModel