Created
November 2, 2016 15:08
-
-
Save robintema/a2dee269041325d19d47d78853ed4572 to your computer and use it in GitHub Desktop.
Django Rest Framework custom pagination class
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, _positive_int | |
class CustomPageNumberPagination(PageNumberPagination): | |
""" A custom DRF paginator class | |
""" | |
# Client can control the page using this query parameter. | |
page_query_param = 'page' | |
# Client can control the page size using this query parameter. | |
# Default is 'None'. Set to eg 'page_size' to enable usage. | |
page_size_query_param = 'page_size' | |
# Set to an integer to limit the maximum page size the client may request. | |
# Only relevant if 'page_size_query_param' has also been set. | |
max_page_size = 500 | |
def get_page_size(self, request): | |
if self.page_size_query_param in request.query_params: | |
try: | |
return _positive_int( | |
request.query_params[self.page_size_query_param], | |
strict=True, | |
cutoff=self.max_page_size | |
) | |
except (KeyError, ValueError): | |
pass | |
return self.page_size |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment