Skip to content

Commit 107e8b3

Browse files
matteiusCarlton Gibson
authored andcommitted
Make DEFAULT_PAGINATION_CLASS None by default. (#5170)
* Changes to the paginator defaults and settings Require a default paginator be specified when using the page size setting. #5168 * DRF-5168 import warnings missed this in last commit * Add a system checks file Add a check for pagination settings for the 3.7 upgrade cycle. * more compatible import approach * missing bactic * revised language and approach to import the system check Adds a rest framework app config. * Adjust doc wording
1 parent 11e5851 commit 107e8b3

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

docs/api-guide/pagination.md

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

2222
## Setting the pagination style
2323

24-
The default 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:
24+
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:
2525

2626
REST_FRAMEWORK = {
2727
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
2828
'PAGE_SIZE': 100
2929
}
3030

31-
Note that you need to set both the pagination class, and the page size that should be used.
31+
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.
3232

3333
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.
3434

@@ -85,7 +85,7 @@ This pagination style accepts a single number page number in the request query p
8585

8686
#### Setup
8787

88-
To enable the `PageNumberPagination` style globally, use the following configuration, modifying the `PAGE_SIZE` as desired:
88+
To enable the `PageNumberPagination` style globally, use the following configuration, and set the `PAGE_SIZE` as desired:
8989

9090
REST_FRAMEWORK = {
9191
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',

rest_framework/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@
2121

2222
# Default datetime input and output formats
2323
ISO_8601 = 'iso-8601'
24+
25+
default_app_config = 'rest_framework.apps.RestFrameworkConfig'

rest_framework/apps.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.apps import AppConfig
2+
3+
4+
class RestFrameworkConfig(AppConfig):
5+
name = 'rest_framework'
6+
verbose_name = "Django REST framework"
7+
8+
def ready(self):
9+
# Add System checks
10+
from .checks import pagination_system_check # NOQA

rest_framework/checks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from django.core.checks import Tags, Warning, register
2+
3+
4+
@register(Tags.compatibility)
5+
def pagination_system_check(app_configs, **kwargs):
6+
errors = []
7+
# Use of default page size setting requires a default Paginator class
8+
from rest_framework.settings import api_settings
9+
if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS:
10+
errors.append(
11+
Warning(
12+
"You have specified a default PAGE_SIZE pagination rest_framework setting,"
13+
"without specifying also a DEFAULT_PAGINATION_CLASS.",
14+
hint="The default for DEFAULT_PAGINATION_CLASS is None. "
15+
"In previous versions this was PageNumberPagination",
16+
)
17+
)
18+
return errors

rest_framework/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
'DEFAULT_VERSIONING_CLASS': None,
5252

5353
# Generic view behavior
54-
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
54+
'DEFAULT_PAGINATION_CLASS': None,
5555
'DEFAULT_FILTER_BACKENDS': (),
5656

5757
# Throttling

0 commit comments

Comments
 (0)