Registry / web-framework / djangorestframework-datatables

djangorestframework-datatables

JSON →
library0.7.2pypypiunverified

This package provides seamless integration between Django REST framework and Datatables. It automatically handles searching, filtering, ordering, and pagination for your DRF API endpoints when requested with the `?format=datatables` parameter. The library, currently at version 0.7.2, maintains an active development status with regular updates to support newer versions of Django, DRF, and Python.

pip install djangorestframework-datatables
INSTALL
IMPORT
SIG · DJANGORESTFRAMEWOR
D
djangorestframework-datatables
web-frameworkpythonv0.7.2
Install
3.6s avg
Import
Disk
70MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.7.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.915 runs
installs and imports cleanly · install 0.0s · import 0.000s · 70.7MB
glibc
py 3.103.915 runs
installs and imports cleanly · install 3.6s · import 0.000s · 71MB
70MB installed
● package 70MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DatatablesRenderer
from rest_framework_datatables.renderers import DatatablesRenderer
from rest_framework_datatables.renderers import DatatablesRenderer

This quickstart demonstrates the core configuration for `djangorestframework-datatables`. It involves adding `rest_framework_datatables` to `INSTALLED_APPS`, and configuring `DEFAULT_RENDERER_CLASSES`, `DEFAULT_FILTER_BACKENDS`, and `DEFAULT_PAGINATION_CLASS` in your `REST_FRAMEWORK` settings. An example Django model, DRF serializer, and ViewSet are provided, along with the necessary URL routing.

import os import django from django.conf import settings from django.urls import path, include from django.db import models from rest_framework import serializers, viewsets, routers os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') # Minimal Django settings for demonstration if not settings.configured: settings.configure( INSTALLED_APPS=[ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'rest_framework', 'rest_framework_datatables' # Add this line ], DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:'}}, ROOT_URLCONF=__name__, SECRET_KEY='a-very-secret-key', REST_FRAMEWORK={ 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.JSONRenderer', 'rest_framework.renderers.BrowsableAPIRenderer', 'rest_framework_datatables.renderers.DatatablesRenderer', # Add this renderer ), 'DEFAULT_FILTER_BACKENDS': ( 'rest_framework_datatables.filters.DatatablesFilterBackend', # Add this filter backend ), 'DEFAULT_PAGINATION_CLASS': 'rest_framework_datatables.pagination.DatatablesPageNumberPagination', 'PAGE_SIZE': 10, } ) django.setup() # 1. Define a simple Django model class Item(models.Model): name = models.CharField(max_length=100) value = models.IntegerField() def __str__(self): return self.name # 2. Define a DRF Serializer class ItemSerializer(serializers.ModelSerializer): class Meta: model = Item fields = ['id', 'name', 'value'] # 3. Define a DRF ViewSet class ItemViewSet(viewsets.ModelViewSet): queryset = Item.objects.all() serializer_class = ItemSerializer # 4. Register the ViewSet with a router router = routers.DefaultRouter() router.register(r'items', ItemViewSet) # 5. Define URL patterns urlpatterns = [ path('api/', include(router.urls)), ] # Example usage (simulate data and API call) if __name__ == '__main__': # Create some dummy data Item.objects.create(name='Apple', value=10) Item.objects.create(name='Banana', value=20) Item.objects.create(name='Cherry', value=30) print("Configuration done. Your API endpoint is '/api/items/'.") print("To get Datatables-compatible output, append '?format=datatables'.") print("Example API call: http://localhost:8000/api/items/?format=datatables") print("Start a Django dev server and navigate to the URL to see output.") # In a real Django project, you would run: # python manage.py makemigrations # python manage.py migrate # python manage.py runserver
Debug
Known issues
gotchaFailing to append `?format=datatables` to your API URL when making requests from Datatables JavaScript will cause an 'Ajax error' in Datatables. The library requires this parameter to format the response correctly.
fix
Always include `?format=datatables` in the AJAX URL configuration for your Datatables instance (e.g., `ajax: { url: '/api/mydata/?format=datatables' }`).
affects: All
gotchaWhen integrating with `django-filter`, the import paths for `DatatablesFilterBackend` and `DatatablesFilterSet` change to use the `django_filters` subpackage. Importing from the top-level `filters` module will not provide the enhanced functionality.
fix
Update imports from `rest_framework_datatables.filters` to `rest_framework_datatables.django_filters.backends` for `DatatablesFilterBackend` and `rest_framework_datatables.django_filters.filterset` for `DatatablesFilterSet`.
affects: 0.6.0+
gotchaColumn names in your Datatables JavaScript configuration (`data` or `name` properties) must exactly match the fields exposed by your DRF serializer. Mismatches lead to empty columns or 'Ajax error'.
fix
Ensure `data` attributes in Datatables column definitions correspond to your `Serializer` fields (e.g., `{'data': 'name', 'name': 'name'}` for a `name` field in the serializer).
affects: All
Upgrade
Version history
0.7.2latest on PyPI · released Jun 14, 2024
Audit
Dependencies
DjangorequiredCore framework dependency, version 3.2, 4.1, 4.2 supported.
djangorestframeworkrequiredCore REST framework dependency, version 3.14 supported.
django-filteroptionalOptional, for more fine-grained control over database queries and advanced filtering capabilities.
Agent activity
17 hits · last 30 days
node
16
Amazon
1
Resources
djangorestframework-datatables — pip install djangorestframework-datatables · libregistry