Add custom pagination to return total number of pages.
This commit is contained in:
parent
9a9cf4c6ef
commit
586391f19d
2 changed files with 21 additions and 1 deletions
20
savepointradio/api/pagination.py
Normal file
20
savepointradio/api/pagination.py
Normal 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
|
||||||
|
})
|
|
@ -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',
|
||||||
),
|
),
|
||||||
|
|
Loading…
Reference in a new issue