Pagination not working in DRF APIView

2019-03-06 03:28发布

I am using APIView for get and post items.
I wanted to implement pagination for my API using Django Rest Framework, but it is not working.

I want to show 10 items per page but when I do api/v1/items?page=1, I get all the items and if I just do api/v1/items I get an empty list.

Here is what I have done:

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

class ItemsAPIView(APIView):
    permission_classes = (permissions.IsAuthenticated,)

    def get(self, request, format=None):
        """
        Return a list of all items of this user.
        """
        reply = {}
        page = request.GET.get('page')
        print ('page is', page)
        try:
            products = BaseItem.objects.owned_items().filter(owner=request.user)
            reply['data'] = OwnedItemSerializer(products, many=True).data

            items = BaseItem.objects.filter(owner=request.user)
            paginator = Paginator(items, 1)
            items_with_pagination = paginator.page(page)
            if page is not None:
                reply['data'].extend(ItemSerializer(items_with_pagination, many=True).data)
            reply['data'].extend(ItemSerializer(items, many=True).data)

4条回答
混吃等死
2楼-- · 2019-03-06 04:04

paginator = None worked for me

> class NotesViewSet(viewsets.ModelViewSet):    
>     queryset = Notes.objects.all()
>     serializer_class = NotesWriteSerializer
>     paginator = None
查看更多
倾城 Initia
3楼-- · 2019-03-06 04:09

You could make your life a lot easier and just use a ListView. By overriding render_to_response you can make it output json instead of HTML. It does all the pagination for you.

from django.core import serializers
from django.views.generic import ListView

class MyListView(JSONResponseMixin, ListView):
    model = models.BaseItem    
    paginate_by = 10  

    def render_to_response(self, context):
        return JsonResponse(
            serializers.serialize("json", context),
            **response_kwargs
        )
查看更多
\"骚年 ilove
4楼-- · 2019-03-06 04:10

Non generic views and viewsets do not have pagination by default as stated in the django rest framework documentation:

Pagination is only performed automatically if you're using the generic views or viewsets. If you're using a regular APIView, you'll need to call into the pagination API yourself to ensure you return a paginated response. See the source code for the mixins.ListModelMixin and generics.GenericAPIView classes for an example.

I have composed a full example on enabling pagination on non generic views with code on how to achieve this:

class ItemsAPIView(APIView):
    permission_classes = (permissions.IsAuthenticated,)
    pagination_class = api_settings.DEFAULT_PAGINATION_CLASS
    serializer_class = MyNewUnifiedSerializerClass

    def get(self, request):
        user_items =  BaseItem.objects.filter(
                          owner=request.user
                      ).distinct()
        page = self.paginate_queryset(user_items)

        if page is not None:
            serializer = self.serializer_class(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(user_items, many=True)
        return Response(serializer.data)

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

For more details have a look at the example link provided.

查看更多
我只想做你的唯一
5楼-- · 2019-03-06 04:12

extend GenericAPIView instead of APIView and define pagination_class in GenericAPIView

查看更多
登录 后发表回答