3

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)
Sara
  • 69
  • 2
  • 9
  • Watch out with self.request.query_params.get('page') - if no page is specified your endpoint will return all objects in queryset :) Usually in such case it is nice to return just first page. so ...get('page', 1) – opalczynski Feb 17 '17 at 21:56
  • Thank you for your friendly reminder :D I will have to make `page` optional because our FE doesn't support pagination now. – Sara Feb 17 '17 at 22:13

2 Answers2

7

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.

opalczynski
  • 1,599
  • 12
  • 14
0

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