Skip to content

Commit 7a70dc1

Browse files
Add MAX_PAGE_SIZE setting
1 parent 4bbfa8d commit 7a70dc1

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

docs/api-guide/pagination.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ Pagination can be turned off by setting the pagination class to `None`.
2424

2525
## Setting the pagination style
2626

27-
The pagination style may be set globally, using the `DEFAULT_PAGINATION_CLASS` and `PAGE_SIZE` setting keys. For example, to use the built-in limit/offset pagination, you would do something like this:
27+
The pagination style may be set globally, using the `DEFAULT_PAGINATION_CLASS`, `PAGE_SIZE` and `MAX_PAGE_SIZE` setting keys. For example, to use the built-in limit/offset pagination, you would do something like this:
2828

2929
REST_FRAMEWORK = {
3030
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
31-
'PAGE_SIZE': 100
31+
'PAGE_SIZE': 100,
32+
'MAX_PAGE_SIZE': 250,
3233
}
3334

34-
Note that you need to set both the pagination class, and the page size that should be used. Both `DEFAULT_PAGINATION_CLASS` and `PAGE_SIZE` are `None` by default.
35+
Note that you need to set both the pagination class, and the page size and limit that should be used.
36+
`DEFAULT_PAGINATION_CLASS`, `PAGE_SIZE`, `MAX_PAGE_SIZE` are `None` by default.
3537

3638
You can also set the pagination class on an individual view by using the `pagination_class` attribute. Typically you'll want to use the same pagination style throughout your API, although you might want to vary individual aspects of the pagination, such as default or maximum page size, on a per-view basis.
3739

rest_framework/pagination.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ class PageNumberPagination(BasePagination):
173173
# The default page size.
174174
# Defaults to `None`, meaning pagination is disabled.
175175
page_size = api_settings.PAGE_SIZE
176+
# The maximum page size.
177+
# Defaults to `None`, meaning page size is unlimited.
178+
# It's recommended that you would set a limit to avoid api abuse.
179+
page_size = api_settings.MAX_PAGE_SIZE
176180

177181
django_paginator_class = DjangoPaginator
178182

@@ -383,7 +387,7 @@ class LimitOffsetPagination(BasePagination):
383387
limit_query_description = _('Number of results to return per page.')
384388
offset_query_param = 'offset'
385389
offset_query_description = _('The initial index from which to return the results.')
386-
max_limit = None
390+
max_limit = api_settings.MAX_PAGE_SIZE
387391
template = 'rest_framework/pagination/numbers.html'
388392

389393
def paginate_queryset(self, queryset, request, view=None):

rest_framework/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565

6666
# Pagination
6767
'PAGE_SIZE': None,
68+
"MAX_PAGE_SIZE": None,
6869

6970
# Filtering
7071
'SEARCH_PARAM': 'search',

0 commit comments

Comments
 (0)