user2492364
user2492364

Reputation: 6693

Django Rest Framework set the pagination_class on an individual API

I set DEFAULT_PAGINATION_CLASS in settings.py

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 100,
}

And I have an api need to return all data in database without paginator
I set pagination_class = LimitOffsetPagination

views.py

class LayerList(generics.ListAPIView):
    queryset = Layer.objects.all()
    serializer_class = LayerSerializer
    pagination_class = LimitOffsetPagination
    filter_class = LayerFilter
    filter_backends = (filters.DjangoFilterBackend,)

But this api stiil have pagination function :

{
    "count": 18,
    "next": null,
    "previous": null,
    "results": [
        {}
    ]
}    

What else should I setting???

UDDATE

LimitOffsetPagination is still a Pagination explained by @AkramParvez

Upvotes: 1

Views: 2157

Answers (2)

ilse2005
ilse2005

Reputation: 11429

If you want ro return all data withou paginator you should set pagination_class=None. Then you don't use pagination at all. Is it that what you want to achieve?

Upvotes: 3

Akram Parvez
Akram Parvez

Reputation: 451

Both the apis return the same pagination json which contain count, next, previous and results keys. The difference is in the request query parameters. PageNumberPagination uses https://api.example.org/accounts/?page=4 and returns

HTTP 200 OK
{
    "count": 1023
    "next": "https://api.example.org/accounts/?page=5",
    "previous": "https://api.example.org/accounts/?page=3",
    "results": [
       …
    ]
}

While LimitOffsetPagination uses https://api.example.org/accounts/?limit=100&offset=400 and returns

HTTP 200 OK
{
    "count": 1023
    "next": "https://api.example.org/accounts/?limit=100&offset=500",
    "previous": "https://api.example.org/accounts/?limit=100&offset=300",
    "results": [
       …
    ]
}

Upvotes: 0

Related Questions