
Django Rest Framework provides several built-in ways to handle pagination of your API responses. Here are the most commonly used methods for pagination in Django Rest Framework:
1. PageNumberPagination: This is the simplest and most straightforward way of pagination. You specify the number of items you want in a single page, and the API will return the results accordingly. To use PageNumberPagination, you need to include the following code in your settings.py
file:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
}
2. LimitOffsetPagination: This is similar to PageNumberPagination but instead of specifying the number of items per page, you specify the starting point and the number of items to return. To use LimitOffsetPagination, you need to include the following code in your settings.py
file:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'DEFAULT_LIMIT': 10,
}
3. CursorPagination: This is a more efficient way of pagination, particularly when working with large datasets. It uses a cursor-based approach and returns a cursor that you can use to fetch the next set of results. To use CursorPagination, you need to include the following code in your settings.py
file:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.CursorPagination',
'PAGE_SIZE': 10,
}
You can also create your own custom pagination class if the built-in classes do not meet your needs. To use a custom pagination class, you need to include the following code in your settings.py
file:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'path.to.CustomPaginationClass',
}
A simple project on PageNumber pagination in Django:
1. Start by creating a new Django project using the following command:
django-admin startproject pagination_project
2. Create a new Django app within the project using the following command:
python manage.py startapp pagination_app
3. Install the Django Rest Framework by running the following command:
pip install djangorestframework
4. Add the following code to your settings.py
file to specify the pagination class:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 5,
}
5. Add the Django Rest Framework to your INSTALLED_APPS
in your settings.py
file:
INSTALLED_APPS = [ ... 'rest_framework', 'pagination_app',]
6. Create a model in your models.py
file:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
published_date = models.DateField()
7. Create a serializer for your model in your serializers.py
file:
from rest_framework import serializers
from pagination_app.models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
8. Create a view in your views.py
file:
from rest_framework import generics
from pagination_app.models import Book
from pagination_app.serializers import BookSerializer
class BookList(generics.ListAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
9. Add a URL pattern to your urls.py
file:
from django.urls import path
from pagination_app.views import BookList
urlpatterns = [
path('books/', BookList.as_view(), name='book-list'),
]
10 . Run the Django development server using the following command:
python manage.py runserver
11. You can now access the API endpoint by visiting http://localhost:8000/books/
in your web browser. You should see a paginated list of books in your database.