Reputation: 48
Recently I was working with drf pagination class,PageNumberPagination.I may or may not have encountered a weird bug . The official docs mention,to overide the page size of PageNumberPagination we have to create a Custom paginator which overides the page size configuration like shown below
class StandardResultsSetPagination(PageNumberPagination):
page_size = 100
page_size_query_param = 'page_size'
max_page_size = 1000
class BillingRecordsView(generics.ListAPIView):
queryset = Billing.objects.all()
serializer_class = BillingRecordsSerializer
pagination_class = LargeResultsSetPagination
But when I tried to do the same the custom paginator was using the default setting as 100 Here's my snippet I used
class StandardResultsSetPagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 100
class TrendingClassesView(ListAPIView):
pagination_class = StandardResultsSetPagination
serializer_class = BaseClassTileSerializer
queryset = BaseClass.objects.all()
One moment the code the working fine but after playing around with the page size for some time the paginator just stopped working, I have to do something like below to make the page size working
class StandardResultsSetPagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 100
def get_page_size(self, request):
return 10
This is my rest framework settings
REST_FRAMEWORK = {
'UPLOADED_FILES_USE_URL':
True,
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.MultiPartParser',
'rest_framework.parsers.FileUploadParser',
'rest_framework.parsers.FormParser',
],
"TIME_FORMAT":
"%I:%M %p",
"TIME_INPUT_FORMATS": ["%I:%M %p", "%H:%M"],
}
I guess I may be doing something wrong but I am not able to find the mistake.
Upvotes: 0
Views: 1516
Reputation: 48
Yeah,this was probably dumb but anyways I figured out the issue .The problem wasn't with the given two views so I didn't provide proper info here.After writing the above comment I started looking for if I am overiding the page size somewhere else So I found this
class FocusView(ListAPIView):
authentication_classes = (JWTAuthentication, TokenAuthentication,
SessionAuthentication, BasicAuthentication)
permission_classes = (IsAuthenticated, IsCustomer, )
renderer_classes = (
JSONRenderer,
)
pagination_class = StandardResultsSetPagination
serializer_class = FocusTileSerializer
pagination_class.page_size=100 #this line was the culprit here
This view for just below the view I used above , so when the server reload python loads all the modules and files so this view was overiding the page_size of the Custom paginator.I don't really know if it is even supposed to do that,but it surely should be mentioned in docs or maybe I missed it.
Upvotes: 1
Reputation: 997
I don't see any mistake in custom pagination, although you do not have to override the get method. Try setting default pagination class in settings.
REST_FRAMEWORK = {
'UPLOADED_FILES_USE_URL':
True,
'DEFAULT_PARSER_CLASSES': [
'rest_framework.parsers.JSONParser',
'rest_framework.parsers.MultiPartParser',
'rest_framework.parsers.FileUploadParser',
'rest_framework.parsers.FormParser',
],
"TIME_FORMAT":
"%I:%M %p",
"TIME_INPUT_FORMATS": ["%I:%M %p", "%H:%M"],
'DEFAULT_PAGINATION_CLASS': 'your.path.StandardResultsSetPagination'
}
It's mentioned in the documentation too : DRF pagination
PS: Please check the queryset and viewset too, it's possible that the bug might be from the query you are fetching the results.
Upvotes: 0