Last active
October 27, 2021 00:14
-
-
Save ZoeLiao/72b83fca15398caac5cd97db6eb9d438 to your computer and use it in GitHub Desktop.
Django REST framework customized pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), # can not set default = self.page | |
'page_size': int(self.request.GET.get('page_size', self.page_size)), | |
'results': data | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Gwamaka, thank you for your feedback. I am sorry that I can't share the whole project because it is a private repo.