Skip to content

Giftovus/drf-link-pagination

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DRF Link Pagination

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.

How do I use this?

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"}, ...]

Tests

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

Contributing

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.

About

Add `Link`-based pagination to your API.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published