Pagination, data filtering, and search in Django

To implement pagination, data filtering, and search in Django with Django REST Framework (DRF), you can take advantage of several built-in and third-party tools. Here’s how you can implement each feature:

1. Pagination

Django REST Framework provides a built-in pagination system. You can add pagination globally (for all views) or to specific views.

Global Pagination (in settings.py):

To enable pagination globally for all your API views:

# settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10,  # Number of items per page
}

View-Specific Pagination:

You can apply pagination to specific views by overriding the pagination_class.

from rest_framework.pagination import PageNumberPagination
from rest_framework import generics
from .models import MyModel
from .serializers import MyModelSerializer

class MyModelPagination(PageNumberPagination):
page_size = 5 # Items per page
page_size_query_param = ‘page_size’ # Allows dynamic page size via query param
max_page_size = 100 # Limits the maximum page size

class MyModelListView(generics.ListAPIView):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
pagination_class = MyModelPagination # Apply custom pagination

In this example, you can search for data using a query parameter:

GET /mymodel/?page=2

2. Data Filtering

For data filtering, you can use DjangoFilterBackend from the django-filter package. It allows you to filter queryset results based on fields in the model.

Install the required package:

pip install django-filter

Add FilterBackend to settings.py:

settings.py

REST_FRAMEWORK = {
‘DEFAULT_FILTER_BACKENDS’: [‘django_filters.rest_framework.DjangoFilterBackend’],
}

Example Implementation:

from rest_framework import generics
from django_filters.rest_framework import DjangoFilterBackend
from .models import MyModel
from .serializers import MyModelSerializer

class MyModelListView(generics.ListAPIView):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    filter_backends = [DjangoFilterBackend]  # Apply filter backend
    filterset_fields = ['name', 'category']  # Fields that can be filtered

In this example, you can filter data using query parameters:

3. Search Functionality
Django REST Framework provides a SearchFilter that allows you to search across specific fields in the model using query parameters.

Add SearchFilter:

settings.py

REST_FRAMEWORK = {
‘DEFAULT_FILTER_BACKENDS’: [‘rest_framework.filters.SearchFilter’],
}

from rest_framework import generics
from rest_framework.filters import SearchFilter
from .models import MyModel
from .serializers import MyModelSerializer

class MyModelListView(generics.ListAPIView):
    queryset = MyModel.objects.all()
    serializer_class = MyModelSerializer
    filter_backends = [SearchFilter]  # Apply search filter
    search_fields = ['name', 'description']  # Fields to search in

from rest_framework import generics
from rest_framework.filters import SearchFilter
from .models import MyModel
from .serializers import MyModelSerializer

class MyModelListView(generics.ListAPIView):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer
filter_backends = [SearchFilter] # Apply search filter
search_fields = [‘name’, ‘description’] # Fields to search in

Assume you have the following request examples:

  • Pagination: GET /mymodel/?page=2
  • Filtering: GET /mymodel/?name=example&category=tech
  • Search: GET /mymodel/?search=example

Summary:

  • Pagination: Use PageNumberPagination or customize it for your needs.
  • Filtering: Use DjangoFilterBackend for model field filtering.
  • Search: Use SearchFilter to enable search across specific fields.
  • Combining: You can easily combine pagination, filtering, and search in a single view.

This combination will give your API rich querying capabilities, improving user experience when dealing with large datasets.

Leave a Reply

Your email address will not be published. Required fields are marked *

Deprecated: htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/wp-includes/formatting.php on line 4720