[Answered ]-DjangoRestFramework – How to serialize only certain number of objects when ViewSet GET / LIST is called?

1👍

You’ll want to use the pagination controls that Django Rest Framework provides just like you guessed.

This snippit might help you wrap your head around it:

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

It is showing a response that, in addition to the normal ‘results’ that you would expect also includes next and previous values. These are links to the end points that can be used to return the results for the previous page worth of data, or the next page worth of data. It is the front end’s responsibility to map these links to the appropriate user controls so that the pagination navigation can occur.

1👍

You are right. You need Pagination to achieve this. Just include pagination_class like you wrote serializer_class etc. Also, create a class which will have the number, with which you wish to paginate.

from rest_framework.pagination import PageNumberPagination
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 1000

and set pagniation_class = StandardResultsSetPagination.

Leave a comment