Sara
Sara

Reputation: 79

Django Rest Framework global pagination and pagination_class are not working

My settings:

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 2
}

My pagination Class:

from rest_framework.pagination import PageNumberPagination


class CustomNumberPagination(PageNumberPagination):
    page_size = 5

My Testing View Class:

from rest_framework.pagination import PageNumberPagination
from .pagination import CustomNumberPagination


class Testing(generics.GenericAPIView):

    queryset = Testing.objects.all()
    serializer_class = TestingSerializer
    pagination_class = CustomNumberPagination

    def get(self, request):
        print PageNumberPagination.page_size  # 2
        print self.pagination_class.page_size  # 5
        queryset = self.get_queryset()
        serializer = self.serializer_class(queryset, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

I can print out the page_size of PageNumberPagination and CustomNumberPagination in my console correctly.

However, passing page as a parameter doesn't have any effect. I couldn't get either global paginations or pagination_class in each view to work. I am not sure what went wrong, but it seems that most people did the same thing and just worked for them. I'd appreciate any suggestions for me.

Updates

Just got some inspirations from my selected answer below.

Since I will have to write a lot of customizations in my overwritten get(), I just updated my get():

from rest_framework.pagination import PageNumberPagination
from .pagination import CustomNumberPagination


class Testing(generics.GenericAPIView):

    queryset = Testing.objects.all()
    serializer_class = TestingSerializer
    pagination_class = CustomNumberPagination

    def get(self, request):
        queryset = self.get_queryset()
        page = self.request.query_params.get('page')
        if page is not None:
            paginate_queryset = self.paginate_queryset(queryset)
            serializer = self.serializer_class(paginate_queryset, many=True)
            return self.get_paginated_response(serializer.data)

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

Upvotes: 4

Views: 8123

Answers (2)

user14475872
user14475872

Reputation:

In my case, I was passing wrong argument in query param for page no. It was page but I was passing page_no

Upvotes: 0

opalczynski
opalczynski

Reputation: 1647

Take a look how it is done in drf itself:

class ListModelMixin(object):
    """
    List a queryset.
    """
    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

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

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

Hope that this will help you - as is self-explanatory;

You used GenericAPIView - and overwrite the get - you should use the get_paginated_response method to achieve pagination.

Happy coding.

Upvotes: 8

Related Questions