[Fixed]-How to set dynamic page_size in DRF PageNumberPagination

36👍

Create a new Pagination class from PageNumberPagination and override page_size_query_param attribute as below

from rest_framework.pagination import PageNumberPagination


class CustomPageNumberPagination(PageNumberPagination):
    page_size_query_param = 'size'  # items per page

Hence your URL will be, /api/foo/?size=10 , this will return 10 items per page. If you not providing the size argument in the URL, DRF will use settings.PAGE_SIZE

👤JPG

Leave a comment