I am having problem with django rest framework pagination. I have set pagination in settings like -
'DEFAULT_PAGINATION_CLASS':'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 1
Below is my viewset.
class HobbyCategoryViewSet(viewsets.ModelViewSet):
serializer_class = HobbyCategorySerializer
queryset = UserHobbyCategory.objects.all()
I want to set different page size for this viewset. I have tried setting page_size and Paginate_by class variables but list is paginated according to PAGE_SIZE defined in settings. Any idea where I am wrong ?
In case you are trying to change the "page size" of the LimitOffsetPagination class, you have to override the default_limit variable instead of page_size:
In the case that you want to set a default pagination value, including a max and a param, here is what you do.
1) Create a
drf_defaults.py
(or any name you choose). I placed it in same dir assettings.py
:2) In your
settings.py
, updateREST_FRAMEWORK
dict, adding the following:In the end my REST_FRAMEWORK settings dict looks like:
Your settings will of course vary! Cheers!
Use page size query params to provide page size dynamically..
Set Default pagination class in settings
Now in your URL provide limit as a GET parameter..
Simplest Solution
For LimitOffsetPagination :
For PageNumberPagination:
For CursorPagination:
Full Code:
Make sure you have DEFAULT_PAGINATION_CLASS in your settings.
I fixed this by creating custom pagination class. and setting desired pagesize in class. I have used this class as pagination_class in my viewset.
I am not sure if there is any easier way for this. this one worked for me. But I think its not good to create new class just to change page_size.
Edit - simple solution is set it like
in ViewSet.