Skip to content Skip to sidebar Skip to footer

Paginating A List Returned By A ViewSet In Django Rest Framework

I have created a ViewSet class with a overridden list method like this: from rest_framework.response import Response from rest_framework import viewsets class MyViewSet(views.View

Solution 1:

You just need to call get_paginated_reponse method on paginator instead of returning Response. If this is a single viewset only

class MyViewSet(views.ViewSet):
    def list(self, request):
        data = [
            {"id": 1},
            {"id": 2},
        ]
        paginator = LinkHeaderPagination()
        page = paginator.paginate_queryset(data, request)
        if page is not None:
            return paginator.get_paginated_response(page)

        return Response(data)

Post a Comment for "Paginating A List Returned By A ViewSet In Django Rest Framework"