简体   繁体   中英

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

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?

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": [
       …
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM