Issue
For some reason, I could not understand the meaning and role of page=DEFAULT_PAGE
in Custom Pagination in the DRF topic. Can you explain its role to me in the code below?
Consider - pagination.py:
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
DEFAULT_PAGE = 9
DEFAULT_PAGE_SIZE = 9
class CustomPagination(PageNumberPagination):
page = DEFAULT_PAGE
page_size = DEFAULT_PAGE_SIZE
page_size_query_param = 'page_size'
def get_paginated_response(self, data):
return Response({
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link()
},
'total': self.page.paginator.count,
'page': int(self.request.GET.get('page', DEFAULT_PAGE)), # can not set default = self.page
'page_size': int(self.request.GET.get('page_size', self.page_size)),
'results': data
})
Note : This is important to me for setting up my app and I wanted to understand its role... Although I consulted the documentation, I didn't understand much about the role of page=PAGE_DEFAULT
.
Solution
The setting page = DEFAULT_PAGE
define a default page number (9 in your example) for the DRF custom pagination class.
When the client doesn't specify one in the request, it ensures a fallback for pagination.
Answered By - Dos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.