How Django rest framework override page size in ViewSet

0 votes

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 ?

Jul 2, 2020 in Python by kartik
• 37,520 points
6,047 views

1 answer to this question.

0 votes

Hello @kartik,

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

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 

Hope it helps!!

Thanks!!

answered Jul 2, 2020 by Niroj
• 82,840 points

Related Questions In Python

0 votes
1 answer

How to add annotate data in django-rest-framework queryset responses?

Hello @kartik, The queryset returned from get_queryset provides ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,840 points
6,146 views
0 votes
1 answer

How to secure APIs for Registration and Login in Django Rest Framework?

Hello @kartik, you cannot have an authentication system ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,840 points
2,901 views
0 votes
1 answer

How to update user password in Django Rest Framework?

Hello @kartik, Using a modelserializer might be an ...READ MORE

answered Jul 1, 2020 in Python by Niroj
• 82,840 points
9,288 views
0 votes
1 answer

How can modify request.data in django REST framework?

Hello @kartik, Generally request in drf views is rest_framework.request.Request instance. Following to ...READ MORE

answered Jul 2, 2020 in Python by Niroj
• 82,840 points
5,484 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
2,506 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,840 points
1,586 views
0 votes
1 answer

How to disable a method in a ViewSet, django-rest-framework

Hello @kartik, You could keep using viewsets.ModelViewSet and define http_method_names on your ...READ MORE

answered Jun 26, 2020 in Python by Niroj
• 82,840 points
10,888 views
0 votes
1 answer

How to return custom JSON in Django REST Framework?

Hello @kartik, There are 2 ways to custom ...READ MORE

answered Jun 25, 2020 in Python by Niroj
• 82,840 points
11,180 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP