Package: django-datatable-serverside-mixin
Django Serverside DataTablesThis is a fork of Umesh Krishna's django-serverside-datatable which was written for an older version of DataTables. This updated version has been rewritten. Thanks to Umesh for the foundation to start from.This is  package that lets you easily extend views to work with DataTables.net server-side processing in your Django Project.Supports DataTables features such as Pagination, Search, filtering, etc...RequirementsPythin version 3.10+ (may work on older versions but is untested)Django version 3.x or 4.xInstallpip install django-datatable-serverside-mixinHow to useCreate a django View that inherits fromServerSideDatatableMixin.
Example (backend):# views.pyfromdjango_serverside_datatable_mixin.viewsimportServerSideDataTablesMixinclassPersonListView(ServerSideDatatableMixin):queryset=Person.objects.all()columns=['person_name','person_code','person_description','person__building__name']# urls.py# add the following element to your urlpatterns arraypath('data/',views.PersonListView.as_view()),Example (frontend):<!DOCTYPE html><html><head><metacharset="utf-8"><linkrel="stylesheet"type="text/css"href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css"/><scripttype="text/javascript"src="https://code.jquery.com/jquery-3.6.0.min.js"></script><scripttype="text/javascript"src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script></head><body><h1>People</h1><hr><tableid="people-table"><thead><tr><th>Name</th><th>Code</th><th>Description</th><th>Building Name</th></tr></thead><tbody></tbody></table><scriptsrc="https://code.jquery.com/jquery-3.3.1.min.js"></script><scriptsrc="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script><scriptlanguage="javascript">$(document).ready(function(){$('#people-table').dataTable({serverSide:true,sAjaxSource:"http://127.0.0.1:8000/data/",// new urlcolumns:[{name:"name",data:"name"},{name:"code",data:"code"},{name:"description",data:"description"},{name:"building",data:"building__name"},],});});</script></body></html>The dataTablescolumnsoption must be configured in the dataTable initialization. Each column isrequiredto have adataattribute corresponding to the view'scolumnsarray. Name is optional as of version 2.0.0 and is no longer used.Thedataattribute must correspond to a valid field provided by the view and must ultimately match an attribute on records in your queryset. Use annotations on your queryset if you prefer data values that don't look like "person__building__name."This is generally compatible with DataTables features such as ColReorder and colvis.For further customization of Datatable, you may refer theDatatables.net official documentation.Data CallbackServerSideDataTablesMixin provides a callback method named data_callback that can be overridden. Use this method to change the formatting or add/remove any pieces of data. This gives you full flexibility to render the dataaftereverything has been sorted, filtered, paginated, etc...classPersonListView(ServerSideDataTablesMixin):defdata_callback(self,data:list[dict])->list[dict]:forrowindata:row["actions"]=render_to_string("people/table_row_buttons.html",context={"person":row},request=self.request,)row["id"]=f"{<b>row['id']</b>"returnsuper().data_callback(data)In the example above the data for the ID column would render withtags to make it bold. The table_row_buttons.html template would render buttons based on the person object. This text is added to therow["actions"]attribute and the javascript would look for a column definition fordata: "actions".UpdatesNew in version 2.1.1:Fix deprecation issues where inheritance would not function as expected.Fixed readme typos.New in version 2.1.0:Renamed ServerSideDatatableMixin to ServerSideDataTablesMixin. A deprecation warning will trigger if the old class name is used.Added a data_callback method to ServerSideDataTablesMixin which can be used to manipulate the data just before rendering to JSON. Useful for adding HTML or modifying any data fields based on view context (like permissions).New in version 2.0.0:Fixed a bug where using a -1 for the pagination length would not work as expected (now provides all records)Implemented regex filteringImplemented column based filteringWrote testsRefactored all code to streamline the process and speed up queries