From e52d31d09b1b4f2697ec03384be9e57f05c24b6e Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 23 May 2017 17:34:23 -0400 Subject: [PATCH 1/7] Changes to the paginator defaults and settings Require a default paginator be specified when using the page size setting. https://github.com/encode/django-rest-framework/issues/5168 --- docs/api-guide/pagination.md | 6 +++--- rest_framework/pagination.py | 11 +++++++++++ rest_framework/settings.py | 2 +- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/api-guide/pagination.md b/docs/api-guide/pagination.md index 888390018c..8c51947885 100644 --- a/docs/api-guide/pagination.md +++ b/docs/api-guide/pagination.md @@ -21,14 +21,14 @@ Pagination can be turned off by setting the pagination class to `None`. ## Setting the pagination style -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: +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: REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', 'PAGE_SIZE': 100 } -Note that you need to set both the pagination class, and the page size that should be used. +Note that you need to set both the pagination class, and the page size that should be used. The `DEFAULT_PAGINATION_CLASS` is set to `None` and disabled by default. 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. @@ -85,7 +85,7 @@ This pagination style accepts a single number page number in the request query p #### Setup -To enable the `PageNumberPagination` style globally, use the following configuration, modifying the `PAGE_SIZE` as desired: +To enable the `PageNumberPagination` style globally, use the following configuration, and set the `PAGE_SIZE` as desired: REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 0255cfc7f2..0f8e6ddfb9 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -146,6 +146,17 @@ def invert(x): class BasePagination(object): display_page_controls = False + def __init__(self, *args, **kwargs): + # Use of default page size setting requires a default Paginator class + if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS: + warnings.warn( + "A valid paginator class must be specified with `DEFAULT_PAGINATION_CLASS` " + "when using the `PAGE_SIZE` default pagination setting." + "Defaulting the setting to specifies `PageNumberPagination` " + "is deprecated as pagination is disabled by default.", + DeprecationWarning + ) + def paginate_queryset(self, queryset, request, view=None): # pragma: no cover raise NotImplementedError('paginate_queryset() must be implemented.') diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 3f3c9110a9..cb5394a362 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -51,7 +51,7 @@ 'DEFAULT_VERSIONING_CLASS': None, # Generic view behavior - 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'DEFAULT_PAGINATION_CLASS': None, 'DEFAULT_FILTER_BACKENDS': (), # Throttling From a224b6b935fcc8365d4f2b248bf8b98442ce731e Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 23 May 2017 17:35:15 -0400 Subject: [PATCH 2/7] DRF-5168 import warnings missed this in last commit --- rest_framework/pagination.py | 1 + 1 file changed, 1 insertion(+) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 0f8e6ddfb9..0b61559420 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -5,6 +5,7 @@ """ from __future__ import unicode_literals +import warnings from base64 import b64decode, b64encode from collections import OrderedDict, namedtuple From 420bc48cfdb5336656cd54d5557703e39618c92e Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Sun, 24 Sep 2017 22:57:05 -0400 Subject: [PATCH 3/7] Add a system checks file Add a check for pagination settings for the 3.7 upgrade cycle. --- rest_framework/__init__.py | 1 + rest_framework/checks.py | 20 ++++++++++++++++++++ rest_framework/pagination.py | 12 ------------ 3 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 rest_framework/checks.py diff --git a/rest_framework/__init__.py b/rest_framework/__init__.py index c0b5c4c041..967f870196 100644 --- a/rest_framework/__init__.py +++ b/rest_framework/__init__.py @@ -6,6 +6,7 @@ | |\ \| |___/\__/ / | | | | | | | (_| | | | | | | __/\ V V / (_) | | | < \_| \_\____/\____/ \_/ |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_| """ +import checks # NOQA __title__ = 'Django REST framework' __version__ = '3.6.3' diff --git a/rest_framework/checks.py b/rest_framework/checks.py new file mode 100644 index 0000000000..96d6f42378 --- /dev/null +++ b/rest_framework/checks.py @@ -0,0 +1,20 @@ +from django.core.checks import Tags, Warning, register + + +@register(Tags.compatibility) +def pagination_system_check(app_configs, **kwargs): + errors = [] + # Use of default page size setting requires a default Paginator class + from rest_framework.settings import api_settings + if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS: + errors.append( + Warning( + "You have specified a default PAGE_SIZE` pagination rest_framework setting," + "without specifying also a `DEFAULT_PAGINATION_CLASS`.", + hint="The prior version of rest_framework defaulted this setting to " + "`PageNumberPagination` however pagination defaults to disabled now. " + "Consider specifying `DEFAULT_PAGINATION_CLASS` explicitly for your project, " + "unless you specify individual pagination_class values on specific view classes.", + ) + ) + return errors diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 0b61559420..0255cfc7f2 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -5,7 +5,6 @@ """ from __future__ import unicode_literals -import warnings from base64 import b64decode, b64encode from collections import OrderedDict, namedtuple @@ -147,17 +146,6 @@ def invert(x): class BasePagination(object): display_page_controls = False - def __init__(self, *args, **kwargs): - # Use of default page size setting requires a default Paginator class - if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS: - warnings.warn( - "A valid paginator class must be specified with `DEFAULT_PAGINATION_CLASS` " - "when using the `PAGE_SIZE` default pagination setting." - "Defaulting the setting to specifies `PageNumberPagination` " - "is deprecated as pagination is disabled by default.", - DeprecationWarning - ) - def paginate_queryset(self, queryset, request, view=None): # pragma: no cover raise NotImplementedError('paginate_queryset() must be implemented.') From 64bc395759ce87d2170beb4ae77e24715f141c18 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Sun, 24 Sep 2017 23:01:02 -0400 Subject: [PATCH 4/7] more compatible import approach --- rest_framework/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/__init__.py b/rest_framework/__init__.py index 967f870196..2b6c5b78d3 100644 --- a/rest_framework/__init__.py +++ b/rest_framework/__init__.py @@ -6,7 +6,7 @@ | |\ \| |___/\__/ / | | | | | | | (_| | | | | | | __/\ V V / (_) | | | < \_| \_\____/\____/ \_/ |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_| """ -import checks # NOQA +from .checks import * # NOQA __title__ = 'Django REST framework' __version__ = '3.6.3' From f34da6dec0c0e912dec953d4929f9ef7248ed80b Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Sun, 24 Sep 2017 23:21:35 -0400 Subject: [PATCH 5/7] missing bactic --- rest_framework/checks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/checks.py b/rest_framework/checks.py index 96d6f42378..7ad51c1d4f 100644 --- a/rest_framework/checks.py +++ b/rest_framework/checks.py @@ -9,7 +9,7 @@ def pagination_system_check(app_configs, **kwargs): if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS: errors.append( Warning( - "You have specified a default PAGE_SIZE` pagination rest_framework setting," + "You have specified a default `PAGE_SIZE` pagination rest_framework setting," "without specifying also a `DEFAULT_PAGINATION_CLASS`.", hint="The prior version of rest_framework defaulted this setting to " "`PageNumberPagination` however pagination defaults to disabled now. " From 7a0a5c38b31901ef58c4790e59e3190b1d70c730 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Mon, 25 Sep 2017 09:16:31 -0400 Subject: [PATCH 6/7] revised language and approach to import the system check Adds a rest framework app config. --- rest_framework/__init__.py | 3 ++- rest_framework/apps.py | 10 ++++++++++ rest_framework/checks.py | 10 ++++------ 3 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 rest_framework/apps.py diff --git a/rest_framework/__init__.py b/rest_framework/__init__.py index 2b6c5b78d3..7a5117769d 100644 --- a/rest_framework/__init__.py +++ b/rest_framework/__init__.py @@ -6,7 +6,6 @@ | |\ \| |___/\__/ / | | | | | | | (_| | | | | | | __/\ V V / (_) | | | < \_| \_\____/\____/ \_/ |_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_| """ -from .checks import * # NOQA __title__ = 'Django REST framework' __version__ = '3.6.3' @@ -22,3 +21,5 @@ # Default datetime input and output formats ISO_8601 = 'iso-8601' + +default_app_config = 'rest_framework.apps.RestFrameworkConfig' diff --git a/rest_framework/apps.py b/rest_framework/apps.py new file mode 100644 index 0000000000..f6013eb7e0 --- /dev/null +++ b/rest_framework/apps.py @@ -0,0 +1,10 @@ +from django.apps import AppConfig + + +class RestFrameworkConfig(AppConfig): + name = 'rest_framework' + verbose_name = "Django REST framework" + + def ready(self): + # Add System checks + from .checks import pagination_system_check # NOQA diff --git a/rest_framework/checks.py b/rest_framework/checks.py index 7ad51c1d4f..af6634d1e3 100644 --- a/rest_framework/checks.py +++ b/rest_framework/checks.py @@ -9,12 +9,10 @@ def pagination_system_check(app_configs, **kwargs): if api_settings.PAGE_SIZE and not api_settings.DEFAULT_PAGINATION_CLASS: errors.append( Warning( - "You have specified a default `PAGE_SIZE` pagination rest_framework setting," - "without specifying also a `DEFAULT_PAGINATION_CLASS`.", - hint="The prior version of rest_framework defaulted this setting to " - "`PageNumberPagination` however pagination defaults to disabled now. " - "Consider specifying `DEFAULT_PAGINATION_CLASS` explicitly for your project, " - "unless you specify individual pagination_class values on specific view classes.", + "You have specified a default PAGE_SIZE pagination rest_framework setting," + "without specifying also a DEFAULT_PAGINATION_CLASS.", + hint="The default for DEFAULT_PAGINATION_CLASS is None. " + "In previous versions this was PageNumberPagination", ) ) return errors From dd8979d7ba6d7b805fb1c7083e87c10687effd2c Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Mon, 25 Sep 2017 15:27:33 +0200 Subject: [PATCH 7/7] Adjust doc wording --- docs/api-guide/pagination.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api-guide/pagination.md b/docs/api-guide/pagination.md index 8c51947885..b0eae47c7d 100644 --- a/docs/api-guide/pagination.md +++ b/docs/api-guide/pagination.md @@ -28,7 +28,7 @@ The pagination style may be set globally, using the `DEFAULT_PAGINATION_CLASS` a 'PAGE_SIZE': 100 } -Note that you need to set both the pagination class, and the page size that should be used. The `DEFAULT_PAGINATION_CLASS` is set to `None` and disabled by default. +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. 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. pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy