Kakaji
Kakaji

Reputation: 1491

How can I use different pagination class per action in Django?

I am using Viewsets. For example, to use different serializer per action I can override get_serializer_class() method. But what about pagination class? Is there any way to set pagination class other than pagination_class = <my pagination class>? I think this is not good as it will change the pagination_class for all the actions.

Upvotes: 2

Views: 394

Answers (1)

You need to override the Pagination class with your own.

from rest_framework.pagination import PageNumberPagination

class PageNumberPaginationDataOnly(PageNumberPagination):
    # Set any other options you want here like page_size

    def get_paginated_response(self, data):
        # implement your logic
        return Response(data)

Upvotes: 1

Related Questions