Django-rest-framework: Paginate a regular APIView API

Created on 12 Jun 2015  路  2Comments  路  Source: encode/django-rest-framework

In docs of DRF only a line is mentioned on this which is given below,

If you're using a regular APIView, you'll need to call into the pagination API yourself to ensure you return a paginated response

I tried and my code is given below. Can someone give a clear reference or piece of code to clearly understand it.

My code

class StandardResultsSetPagination(PageNumberPagination):
    page_size = 1
    page_size_query_param = 'page_size'
    max_page_size = 1000

class EventList(APIView): 
    def get(self, request, format=None):
            filterargs = {} 
        filtered_event_list = Event.objects.filter(**filterargs)
        serializer = EventSerializer(filtered_event_list, many=True, context={'request': request})
        data = {'result': serializer.data}
        pagination_class = StandardResultsSetPagination
        return Response(data)

Most helpful comment

I have the same problem as X17, Can someone give a clear reference or piece of code to clearly understand it?
I have try to fix it according to http://stackoverflow.com/questions/29071312/pagination-in-django-rest-framework-using-api-view. But there is a problem, the reponse return all the data, but not limited by PAGINATE_BY or other settings.
For example, queryset has 17 items and 'PAGINATE_BY' is set to 10 in settings.py:

REST_FRAMEWORK = {
    'PAGINATE_BY': 10, 
}

When I request http://XXX/test-api?page=1, the result contains all the 17 items, but the next is not null, it is http://XXX/test-api?page=2. When I request http://XXX/test-api?page=2, the result is also contains all the 17 items.

Here is my codes:

class FriendCycleList(PaginationAPIView):

        page = self.paginate_queryset(queryset)
        if page is not None:
            print 'page is not None'
            serializer = FriendCycleSerializer(queryset, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = FriendCycleSerializer(queryset, many=True)
        return Response(serializer.data)


class PaginationAPIView(APIView):
    '''
    APIView with pagination
    '''

    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS

    @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)

All 2 comments

Check out the ListModelMixin to see what is done in a viewset. The discussion group is the best place to take this discussion and other usage questions. Thanks!

I have the same problem as X17, Can someone give a clear reference or piece of code to clearly understand it?
I have try to fix it according to http://stackoverflow.com/questions/29071312/pagination-in-django-rest-framework-using-api-view. But there is a problem, the reponse return all the data, but not limited by PAGINATE_BY or other settings.
For example, queryset has 17 items and 'PAGINATE_BY' is set to 10 in settings.py:

REST_FRAMEWORK = {
    'PAGINATE_BY': 10, 
}

When I request http://XXX/test-api?page=1, the result contains all the 17 items, but the next is not null, it is http://XXX/test-api?page=2. When I request http://XXX/test-api?page=2, the result is also contains all the 17 items.

Here is my codes:

class FriendCycleList(PaginationAPIView):

        page = self.paginate_queryset(queryset)
        if page is not None:
            print 'page is not None'
            serializer = FriendCycleSerializer(queryset, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = FriendCycleSerializer(queryset, many=True)
        return Response(serializer.data)


class PaginationAPIView(APIView):
    '''
    APIView with pagination
    '''

    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS

    @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)

Was this page helpful?
0 / 5 - 0 ratings