1

I am trying to use PageNumberPagination in DRF anf for that I have changed the settings.py file like the following:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    # 'PAGE_SIZE':10
}

and views.py

from rest_framework.pagination import PageNumberPagination

class GrnListAPIView(generics.ListAPIView):
    serializer_class = GrnListSerializer
    pagination_class = PageNumberPagination
    permission_classes = (permissions.IsAuthenticated, GRNViewPermission)

    def get_queryset(self):
        return Grn.objects.all()

now when I try to access the data using the URL:

http://localhost:8000/grns/?page=1&page_size=10

It gives me all the data rather than returning 10 objects. It works fine when I uncomment the PAGE_SIZE but only returns 10 values per page, which is understandable as I have mentioned page_size=10 in my params but when I change it to 20 or 30 it still returns the same amount of data instead of 20 or 30 objects.

Rahul Sharma
  • 2,187
  • 6
  • 31
  • 76
  • 3
    It looks like you missed the part where you configure a query parameter name that would be consider as page size. You need to create custom pagination class and inherit from `PageNumberPagination` https://www.django-rest-framework.org/api-guide/pagination/#pagenumberpagination and then specify `page_size_query_param = 'page_size'` like it is done in example https://www.django-rest-framework.org/api-guide/pagination/#modifying-the-pagination-style. The new class must be used in `'DEFAULT_PAGINATION_CLASS'` – wiaterb Sep 08 '21 at 07:04
  • 1
    this is actually an answer. thank you – alexrogo Sep 08 '21 at 18:00

0 Answers0