Skip to content

Instantly share code, notes, and snippets.

@ZoeLiao
Last active October 27, 2021 00:14
Show Gist options
  • Save ZoeLiao/72b83fca15398caac5cd97db6eb9d438 to your computer and use it in GitHub Desktop.
Save ZoeLiao/72b83fca15398caac5cd97db6eb9d438 to your computer and use it in GitHub Desktop.
Django REST framework customized pagination
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
})
@ZoeLiao
Copy link
Author

ZoeLiao commented Oct 27, 2021

Hi Gwamaka, thank you for your feedback. I am sorry that I can't share the whole project because it is a private repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment