Django Rest Framework - APIView Pagination Django Rest Framework - APIView Pagination django django

Django Rest Framework - APIView Pagination


Another option would be inheriting from the pagination class, with fewer changes on the view class:

from rest_framework.pagination import LimitOffsetPaginationclass EventNewsItems(APIView, LimitOffsetPagination):    def get(self, request, pk, format=None):        event = Event.objects.get(pk=pk)        news = event.get_news_items().all()        results = self.paginate_queryset(news, request, view=self)        serializer = NewsItemSerializer(results, many=True)        return self.get_paginated_response(serializer.data)


I have created a Q&A style example on this subject.
As a sort summary:

By utilizing the Django Rest Frameworks source code and how they handle pagination, we create the same methods inside our view class and we use them, in the same way your solution uses the default methods:

Taken from the above mentioned doc:

from rest_framework.settings import api_settingsfrom rest_framework.views import APIViewclass MyView(APIView):    queryset = OurModel.objects.all()    serializer_class = OurModelSerializer    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS # cool trick right? :)    # We need to override get method to achieve pagination    def get(self, request):        ...        page = self.paginate_queryset(self.queryset)        if page is not None:            serializer = self.serializer_class(page, many=True)            return self.get_paginated_response(serializer.data)        ... Do other stuff needed (out of scope of pagination)    # Now add the pagination handlers taken from     #  django-rest-framework/rest_framework/generics.py    @property    def paginator(self):        """        The paginator instance associated with the view, or `None`.        """        if not hasattr(self, '_paginator'):            if self.pagination_class is None:                self._paginator = None            else:                self._paginator = self.pagination_class()        return self._paginator     def paginate_queryset(self, queryset):         """         Return a single page of results, or `None` if pagination is disabled.         """         if self.paginator is None:             return None         return self.paginator.paginate_queryset(queryset, self.request, view=self)     def get_paginated_response(self, data):         """         Return a paginated style `Response` object for the given output data.         """         assert self.paginator is not None         return self.paginator.get_paginated_response(data) 


Another way to paginate is by using the Paginator class.

In addition to the answer query, you must set the number of pages to be displayed and the range of elements that the page will have.

The page number and item range can be provided as part of the request parameters or by the way you select.

Taking as an example the case of the question:

from django.core.paginator import Paginatorclass EventNewsItems(APIView):    def get(self, request, pk, format=None):        #user = request.user        event = Event.objects.get(pk=pk)        news = event.get_news_items().all()         # -----------------------------------------------------------        page_number = self.request.query_params.get('page_number ', 1)        page_size = self.request.query_params.get('page_size ', 10)        paginator = Paginator(news , page_size)        serializer = NewsItemSerializer(paginator.page(page_number) , many=True, context={'request':request})        # -----------------------------------------------------------        response = Response(serializer.data, status=status.HTTP_200_OK)        return response