By default Django REST Framework provides pagination information within the body of a response.
While this has been addressed in the past, the general consensus has
been to make a third-party package until the pagination can be refactored in the
DRF core. Until the refactoring happens, this package provides a very basic
implementation of the Link
header for pagination.
When creating your generic view or viewset, you need to include the
LinkPaginationMixin
towards the beginning. You will also need to set up
pagination on the view using the default pagination serializer.
from rest_framework import viewsets
from rest_link_pagination import mixins
from example.models import ExampleModel
from example.serializers import ExampleSerializer
class ExampleViewSet(mixins.LinkPaginationMixin, viewsets.ModelViewSet):
queryset = ExampleModel.objects.all()
serializer_class = ExampleSerializer
paginate_by = 25
When you make a request to your API, it will return the pagination data in the
Link
header instead of within the body of the response. The response will be
returned as an array instead of an object with the key results
, similar to an
unpaginated response.
HTTP/1.1 200 OK
Link: <http://testserver/generic/list?page=2>; rel="next"
Content-Type: application/json
Allow: GET, HEAD, OPTIONS
Vary: Accept, Cookie
[{"id": 1, "name": "example"}, ...]
The tests can be run through your Django app by running:
python manage.py test rest_link_pagination
or within the project by running:
python rest_link_pagination/tests/run.py
If you see something that needs to be fixed, send us a pull request. If you don't feel like fixing it, or are not sure how to go about it, create an issue on GitHub about it and we can work it out.
The issue tracker is available on GitHub.