Package: djangorestframework-datatables
django-rest-framework-datatablesOverviewThis package provides seamless integration betweenDjango REST frameworkandDatatables.Install django-rest-framework-datatables, call your API with?format=datatablesand it will return a JSON structure that is fully compatible with what Datatables expects.
It handles searching, filtering, ordering and most usecases you can imagine with Datatables.The great benefit of django-rest-framework-datatables is that you don’t have to create a different API, your API still work exactly the same unless you specify thedatatablesformat on your request.Full documentation is available onRead the Docs!You can play with a demo of the example app onPython Anywhere.RequirementsPython (3.7, 3.8, 3.9)Django (2.0, 2.1, 2.2, 3.0, 3.1, 3.2, 4.0)Django REST Framework (3.7, 3.8, 3.9, 3.10, 3.11, 3.12)Please note:Django 3.X branch is only supported with Django REST Framework 3.11 or superior and DRF-datatables version 0.5.1 or superior.Django 4.X branch is only supported with Django REST Framework 3.12 or superior and DRF-datatables version 0.7.0 or superior.QuickstartInstallationJust usepip:$pipinstalldjangorestframework-datatablesConfigurationTo enable Datatables support in your project, add'rest_framework_datatables'to yourINSTALLED_APPS, and modify yourREST_FRAMEWORKsettings like this:REST_FRAMEWORK={'DEFAULT_RENDERER_CLASSES':('rest_framework.renderers.JSONRenderer','rest_framework.renderers.BrowsableAPIRenderer','rest_framework_datatables.renderers.DatatablesRenderer',),'DEFAULT_FILTER_BACKENDS':('rest_framework_datatables.filters.DatatablesFilterBackend',),'DEFAULT_PAGINATION_CLASS':'rest_framework_datatables.pagination.DatatablesPageNumberPagination','PAGE_SIZE':50,}And that’s it !Your API is now fully compatible with Datatables and will provide searching, filtering, ordering and pagination without any modification of your API code !Always Serialize Specific FieldsSometimes you may want to expose fields regardless of datatable’s url parameters. You can do so by setting thedatatables_always_serializetuple like so:classArtistSerializer(serializers.ModelSerializer):id=serializers.IntegerField(read_only=True)classMeta:model=Artistfields=('id','name',)datatables_always_serialize=('id',)An example of Datatable<!doctype html><htmllang="en"><head><metacharset="utf-8"><title>Rolling Stone Top 500 albums of all time</title><linkrel="stylesheet"href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css"><linkrel="stylesheet"href="//cdn.datatables.net/1.10.16/css/dataTables.bootstrap4.min.css"></head><body><divclass="container"><divclass="row"><divclass="col-sm-12"><tableid="albums"class="table table-striped table-bordered"style="width:100%"><thead><tr><th>Rank</th><th>Artist</th><th>Album name</th><th>Year</th><th>Genres</th></tr></thead></table></div></div></div><scriptsrc="//code.jquery.com/jquery-1.12.4.js"></script><scriptsrc="//cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script><scriptsrc="//cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script><script>$(document).ready(function(){vartable=$('#albums').DataTable({"serverSide":true,"ajax":"/api/albums/?format=datatables","columns":[{"data":"rank","searchable":false},{"data":"artist_name","name":"artist.name"},{"data":"name"},{"data":"year"},{"data":"genres","name":"genres.name","sortable":false},]});});</script></body></html>Example projectTo play with the example project, just clone the repository and run the dev server.$gitclonehttps://github.com/izimobil/django-rest-framework-datatables.git$cddjango-rest-framework-datatables$pipinstall-rrequirements-dev.txt$pythonexample/manage.pyrunserver$firefoxhttp://127.0.0.1:8000TestingInstall development requirements.$pipinstall-rrequirements-dev.txtRun the tests.$pythonexample/manage.pytestYou can also use the excellenttoxtesting tool to run the tests
against all supported versions of Python and Django. Install tox
globally, and then simply run:$toxIf you want to check the coverage, use:$coveragerun./example/manage.pytest$coveragereport-mDocumentationThe documentation is available online onRead the Docs.To build the documentation, you’ll need to installsphinx.$pipinstall-rrequirements-docs.txtTo build the documentation:$cddocs$makeclean&&makebuildChangelogVersion 0.7.0 (2021-12-09):Django 4.0 compatibilityAdded global search support to YADCFModelMultipleChoiceFilterVarious fixes on filtersVarious fixes on paginationFixed / improved documentation and examplesMany thanks to all the contributors on this release !Version 0.6.0 (2021-02-09):Integration with django-filterExample of using yadcf and django-filter to create a multi-select columnFixed support for POST requests from datatablesSome fixes on paginationMany thanks to all the contributors on this release !Version 0.5.2 (2020-04-10):Added support for POST requests from datatablesAvoid extra count queriesHandle dummy columns gracefullyVersion 0.5.1 (2020-01-13):Added support for Django 3.0Added support for disabling pagination when the client requests it with length=-1 parameterAdded optional column sorting to handle tiesMinor code fixesVersion 0.5.0 (2019-03-31):Fixed total number of rows when view is using multiple filter back-endsNew meta optiondatatables_extra_jsonon view for adding key/value pairs to rendered JSONMinor docs fixesVersion 0.4.1 (2018-11-16):Added support for Django 2.1 and DRF 3.9Updated READMEVersion 0.4.0 (2018-06-22):Added top level filtering for nested serializersAdded multiple field filteringAdded a ?keep= parameter that allows to bypass the filtering of unused fieldsBetter detection of the requested formatFixed typo in Queryset.count() method nameVersion 0.3.0 (2018-05-11):Added a serializer Meta optiondatatables_always_serializethat allows to specify a tuple of fields that should always be serialized in the response, regardless of what fields are requested in the Datatables requestOptimize filtersUse AND operator for column filtering instead of OR, to be consistant with the client-side behavior of DatatablesVersion 0.2.1 (2018-04-11):This version replaces the 0.2.0 who was broken (bad setup.py)Version 0.2.0 (2018-04-11):Added full documentationRemoved serializers, they are no longer necessary, filtering of columns is made by the rendererVersion 0.1.0 (2018-04-10):Initial release.