Add custom pagination to return total number of pages.

This commit is contained in:
Josh Washburne 2018-04-19 12:08:29 -04:00
parent 9a9cf4c6ef
commit 586391f19d
2 changed files with 21 additions and 1 deletions

View file

@ -0,0 +1,20 @@
from rest_framework import pagination
from rest_framework.response import Response
class TotalPagesPagination(pagination.PageNumberPagination):
'''
Custom pagination class to add the total numer of pages for the results.
(Thanks to https://stackoverflow.com/questions/40985248/)
'''
def get_paginated_response(self, data):
return Response({
'links': {
'next': self.get_next_link(),
'previous': self.get_previous_link()
},
'count': self.page.paginator.count,
'total_pages': self.page.paginator.num_pages,
'results': data
})

View file

@ -102,7 +102,7 @@ REST_FRAMEWORK = {
'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.TokenAuthentication',
), ),
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', 'DEFAULT_PAGINATION_CLASS': 'api.pagination.TotalPagesPagination',
'DEFAULT_PERMISSION_CLASSES': ( 'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated', 'rest_framework.permissions.IsAuthenticated',
), ),