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 ?
Advertisement
Answer
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.
from rest_framework import pagination class ExamplePagination(pagination.PageNumberPagination): page_size = 2 class HobbyCategoryViewSet(viewsets.ModelViewSet): serializer_class = HobbyCategorySerializer queryset = UserHobbyCategory.objects.all() pagination_class=ExamplePagination
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
pagination.PageNumberPagination.page_size = 100
in ViewSet.
class HobbyCategoryViewSet(viewsets.ModelViewSet): serializer_class = HobbyCategorySerializer queryset = UserHobbyCategory.objects.all() pagination.PageNumberPagination.page_size = 100