1

Can we use LimitOffsetPagination with viewsets.ViewSet in Django Rest Framework.

settings.py:

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.TokenAuthentication",
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
    "PAGE_SIZE": 100,
}

Below is how my list view looks:

class UserViewSet(viewsets.ViewSet):

    pagination_class = LimitOffsetPagination

    def get_permissions(self):
        """
        Instantiates and returns the list of permissions that this view requires.
        """
        permission_classes = [IsAuthenticated]

        if self.action == "list" or self.action == "retrieve":
            permission_classes.append(UserReadPermission)
        elif self.action == "create" or self.action == "update":
            permission_classes.append(UserWritePermission)
        elif self.action == "destroy":
            permission_classes.append(UserDeletePermission)

        return [permission() for permission in permission_classes]

    def list(self, request):

        serializer = UserIndexSerializer(data=request.query_params)

        if serializer.is_valid():
            users = User.objects.all()
            serializer = UserGetSerializer(users, many=True)

            response = {"users": serializer.data, "total": len(serializer.data)}

            return AppResponse.success("User list found.", response)

        return AppResponse.error(serializer.errors, None, http_error_code=400)

    def create(self, request):
        pass

    def retrieve(self, request, pk=None):
        pass

    def update(self, request, pk=None):
        pass

    def partial_update(self, request, pk=None):
        pass

    def destroy(self, request, pk=None):
        pass
Pritam Kadam
  • 3,203
  • 4
  • 19
  • 41
  • Just add ```pagination_class = NameOfYourPagination``` to your Viewset (as a class attribute, not in the list() method). ```LimitOffsetPagination``` is to be set in your settings.py along with PAGE_SIZE argument.https://www.django-rest-framework.org/api-guide/pagination/#setting-the-pagination-style – Guillaume Dec 31 '20 at 09:16
  • @BriseBalloches did that but pagination is not working. I have also updated the question adding the entire implementation for viewsets.ViewSet – Pritam Kadam Dec 31 '20 at 09:19
  • Can you edit your question with your settings.py REST_FRAMEWORK variable && the whole Viewset code ? – Guillaume Dec 31 '20 at 09:21
  • @BriseBalloches can't we limit the usage of the pagination class to the view or viewset itself? or do we need to implement it framework wide? – Pritam Kadam Dec 31 '20 at 09:24
  • You can, but you still need to set ```REST_FRAMEWORK = {'PAGE_SIZE': 100}``` in your settings.py otherwise it is considered as None. – Guillaume Dec 31 '20 at 09:29
  • updated the question by adding settings.py REST_FRAMEWORK configuration – Pritam Kadam Dec 31 '20 at 09:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226650/discussion-between-pritam-kadam-and-briseballoches). – Pritam Kadam Dec 31 '20 at 09:34

1 Answers1

1

You can use django-rest default pagination style for entire app using below code in settings.py

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
'PAGE_SIZE': 50 }

If you need pagination per view you can make it as per documentation

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

Then Use it in your View

class viewclass(APIView):
queryset = customquery
serializer_class = serializerclass
pagination_class = LargeResultsSetPagination

Using Parameters can also help like GET https://api.example.org/accounts/?limit=100&offset=400

Asabuas
  • 11
  • 4