Notifications
Clear all
API Development with Django REST Framework
2
Posts
2
Users
0
Reactions
18
Views
0
05/12/2024 5:18 pm
Topic starter
Does I have to register CustomPagination in settings.py file and how ?
2 Answers
0
06/12/2024 8:58 am
Topic starter
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
DEFAULT_PAGE = 1
DEFAULT_PAGE_SIZE = 10
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)),
'page_size': int(self.request.GET.get('page_size', self.page_size)),
'results': data
})
This post was modified 2 weeks ago by Rohan Ekke
0
09/12/2024 2:02 pm
Hi Rohan Ekke,
When creating custom pagination, you need to register your custom pagination in your settings.py
. Here's how you can do it:
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'my_project.apps.core.pagination.CustomPagination',
'PAGE_SIZE': 100
}
Your code is correct, but make sure to replace your_project_name
with the actual name of your project.
If you have any doubts or issues, feel free to ask.