0

I have the following GenericViewSet, I am trying to achieve pagination for the viewset, This is my viewset

class UserAccountViewSet(viewsets.GenericViewSet,
                      mixins.CreateModelMixin,
                      mixins.UpdateModelMixin,
                      mixins.DestroyModelMixin):
    queryset = UserAccount.objects.all()
    lookup_field = 'username'
    lookup_url_kwarg = "username"
    serializer_class = UserAccountSerializer
    page_size = 25
    page_size_query_param = 'page_size'
    max_page_size = 1000

    def list(self, request):
        queryset = self.queryset
        if request.GET.dict():
            return Response(status=status.HTTP_501_NOT_IMPLEMENTED)

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

    def retrieve(self, request, **kwargs):
        pass

    def create(self, request, *args, **kwargs):
        pass

    def update(self, request, *args, **kwargs):
        pass

    def destroy(self, request, *args, **kwargs):
        pass

this is my configuration,

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    ),
    'DEFAULT_THROTTLE_CLASSES': (
        'rest_framework.throttling.AnonRateThrottle',
        'rest_framework.throttling.UserRateThrottle'
    ),
    'DEFAULT_THROTTLE_RATES': {
        'anon': '100/day',
        'user': '100/day'
    }
}

It is not getting paginated, how can I make pagination work with DRF?

thank you.

  • Why are you overriding the `list`, `retrieve` etc functions with just a pass statement? Instead you should just be using `ModelViewSet` in place of `GenericViewSet` without all those functions. – Rahul Gupta May 18 '16 at 06:53
  • Please look into below link, it can help you. http://stackoverflow.com/questions/31785966/django-rest-framework-turn-on-pagination-on-a-viewset-like-modelviewset-pagina – Rahul Pandey May 18 '16 at 07:05
  • @Deena Please add the code of your `list` method. – Rahul Gupta May 18 '16 at 07:10
  • What you are trying to check using `request.GET.dict()`? – Rahul Gupta May 18 '16 at 07:41
  • 1
    The DRF code responsible for pagination is not being called. You should instead call `super()` after that line. – Rahul Gupta May 18 '16 at 07:58

1 Answers1

0

Since you are overriding list() method and not returning a paginated response, you are not getting paginated response in your API. Instead, you should call super() in list() method as DRF's list() method itself returns a paginated response for generic views or viewsets.

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.

class UserAccountViewSet(viewsets.GenericViewSet,
                      mixins.CreateModelMixin,
                      mixins.UpdateModelMixin,
                      mixins.DestroyModelMixin):

    def list(self, request, *args, **kwargs):
        if request.GET.dict():
            return Response(status=status.HTTP_501_NOT_IMPLEMENTED)

        # call 'super()' to get the paginated response
        return super(UserAccountViewSet, self).list(request, *args, **kwargs)
Rahul Gupta
  • 46,769
  • 10
  • 112
  • 126