1
0
Fork 0

Add pagination to API

merge-requests/148/head
Nikos Roussos 2017-03-25 19:08:46 +02:00
parent 0eeeb4cb88
commit 6bbbe1735b
No known key found for this signature in database
GPG Key ID: BADFF1767BA7C8E1
2 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,30 @@
"""
Custom pagination classes for REST framework
"""
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
class LinkedHeaderPageNumberPagination(PageNumberPagination):
"""
This overrides the default PageNumberPagination so that it only
returns the results as an array, not the pagination controls
(eg number of results, etc)
"""
def get_paginated_response(self, data):
next_url = self.get_next_link()
previous_url = self.get_previous_link()
if next_url is not None and previous_url is not None:
link = '<{next_url}>; rel="next", <{previous_url}>; rel="prev"'
elif next_url is not None:
link = '<{next_url}>; rel="next"'
elif previous_url is not None:
link = '<{previous_url}>; rel="prev"'
else:
link = ''
link = link.format(next_url=next_url, previous_url=previous_url)
headers = {'Link': link} if link else {}
return Response(data, headers=headers)

View File

@ -190,7 +190,9 @@ REST_FRAMEWORK = {
),
'DEFAULT_FILTER_BACKENDS': (
'rest_framework.filters.DjangoFilterBackend',
)
),
'DEFAULT_PAGINATION_CLASS': 'db.api.pagination.LinkedHeaderPageNumberPagination',
'PAGE_SIZE': 25
}
# Security