mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-10 07:14:48 +03:00
Merge branch 'MobileWorks-html_cutoff_settings' into version-3-5
This commit is contained in:
commit
f455c67170
|
@ -457,6 +457,8 @@ There are two keyword arguments you can use to control this behavior:
|
||||||
- `html_cutoff` - If set this will be the maximum number of choices that will be displayed by a HTML select drop down. Set to `None` to disable any limiting. Defaults to `1000`.
|
- `html_cutoff` - If set this will be the maximum number of choices that will be displayed by a HTML select drop down. Set to `None` to disable any limiting. Defaults to `1000`.
|
||||||
- `html_cutoff_text` - If set this will display a textual indicator if the maximum number of items have been cutoff in an HTML select drop down. Defaults to `"More than {count} items…"`
|
- `html_cutoff_text` - If set this will display a textual indicator if the maximum number of items have been cutoff in an HTML select drop down. Defaults to `"More than {count} items…"`
|
||||||
|
|
||||||
|
You can also control these globally using the settings `HTML_SELECT_CUTOFF` and `HTML_SELECT_CUTOFF_TEXT`.
|
||||||
|
|
||||||
In cases where the cutoff is being enforced you may want to instead use a plain input field in the HTML form. You can do so using the `style` keyword argument. For example:
|
In cases where the cutoff is being enforced you may want to instead use a plain input field in the HTML form. You can do so using the `style` keyword argument. For example:
|
||||||
|
|
||||||
assigned_to = serializers.SlugRelatedField(
|
assigned_to = serializers.SlugRelatedField(
|
||||||
|
|
|
@ -382,6 +382,22 @@ This should be a function with the following signature:
|
||||||
|
|
||||||
Default: `'rest_framework.views.get_view_description'`
|
Default: `'rest_framework.views.get_view_description'`
|
||||||
|
|
||||||
|
## HTML Select Field cutoffs
|
||||||
|
|
||||||
|
Global settings for [select field cutoffs for rendering relational fields](relations.md#select-field-cutoffs) in the browsable API.
|
||||||
|
|
||||||
|
#### HTML_SELECT_CUTOFF
|
||||||
|
|
||||||
|
Global setting for the `html_cutoff` value. Must be an integer.
|
||||||
|
|
||||||
|
Default: 1000
|
||||||
|
|
||||||
|
#### HTML_SELECT_CUTOFF_TEXT
|
||||||
|
|
||||||
|
A string representing a global setting for `html_cutoff_text`.
|
||||||
|
|
||||||
|
Default: `"More than {count} items..."`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Miscellaneous settings
|
## Miscellaneous settings
|
||||||
|
|
|
@ -18,6 +18,7 @@ from rest_framework.fields import (
|
||||||
Field, empty, get_attribute, is_simple_callable, iter_options
|
Field, empty, get_attribute, is_simple_callable, iter_options
|
||||||
)
|
)
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
|
from rest_framework.settings import api_settings
|
||||||
from rest_framework.utils import html
|
from rest_framework.utils import html
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,14 +72,19 @@ MANY_RELATION_KWARGS = (
|
||||||
|
|
||||||
class RelatedField(Field):
|
class RelatedField(Field):
|
||||||
queryset = None
|
queryset = None
|
||||||
html_cutoff = 1000
|
html_cutoff = None
|
||||||
html_cutoff_text = _('More than {count} items...')
|
html_cutoff_text = None
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.queryset = kwargs.pop('queryset', self.queryset)
|
self.queryset = kwargs.pop('queryset', self.queryset)
|
||||||
self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff)
|
self.html_cutoff = kwargs.pop(
|
||||||
self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text)
|
'html_cutoff',
|
||||||
|
self.html_cutoff or int(api_settings.HTML_SELECT_CUTOFF)
|
||||||
|
)
|
||||||
|
self.html_cutoff_text = kwargs.pop(
|
||||||
|
'html_cutoff_text',
|
||||||
|
self.html_cutoff_text or _(api_settings.HTML_SELECT_CUTOFF_TEXT)
|
||||||
|
)
|
||||||
if not method_overridden('get_queryset', RelatedField, self):
|
if not method_overridden('get_queryset', RelatedField, self):
|
||||||
assert self.queryset is not None or kwargs.get('read_only', None), (
|
assert self.queryset is not None or kwargs.get('read_only', None), (
|
||||||
'Relational field must provide a `queryset` argument, '
|
'Relational field must provide a `queryset` argument, '
|
||||||
|
@ -447,15 +453,20 @@ class ManyRelatedField(Field):
|
||||||
'not_a_list': _('Expected a list of items but got type "{input_type}".'),
|
'not_a_list': _('Expected a list of items but got type "{input_type}".'),
|
||||||
'empty': _('This list may not be empty.')
|
'empty': _('This list may not be empty.')
|
||||||
}
|
}
|
||||||
html_cutoff = 1000
|
html_cutoff = None
|
||||||
html_cutoff_text = _('More than {count} items...')
|
html_cutoff_text = None
|
||||||
|
|
||||||
def __init__(self, child_relation=None, *args, **kwargs):
|
def __init__(self, child_relation=None, *args, **kwargs):
|
||||||
self.child_relation = child_relation
|
self.child_relation = child_relation
|
||||||
self.allow_empty = kwargs.pop('allow_empty', True)
|
self.allow_empty = kwargs.pop('allow_empty', True)
|
||||||
self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff)
|
self.html_cutoff = kwargs.pop(
|
||||||
self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text)
|
'html_cutoff',
|
||||||
|
self.html_cutoff or int(api_settings.HTML_SELECT_CUTOFF)
|
||||||
|
)
|
||||||
|
self.html_cutoff_text = kwargs.pop(
|
||||||
|
'html_cutoff_text',
|
||||||
|
self.html_cutoff_text or _(api_settings.HTML_SELECT_CUTOFF_TEXT)
|
||||||
|
)
|
||||||
assert child_relation is not None, '`child_relation` is a required argument.'
|
assert child_relation is not None, '`child_relation` is a required argument.'
|
||||||
super(ManyRelatedField, self).__init__(*args, **kwargs)
|
super(ManyRelatedField, self).__init__(*args, **kwargs)
|
||||||
self.child_relation.bind(field_name='', parent=self)
|
self.child_relation.bind(field_name='', parent=self)
|
||||||
|
|
|
@ -111,6 +111,10 @@ DEFAULTS = {
|
||||||
'COMPACT_JSON': True,
|
'COMPACT_JSON': True,
|
||||||
'COERCE_DECIMAL_TO_STRING': True,
|
'COERCE_DECIMAL_TO_STRING': True,
|
||||||
'UPLOADED_FILES_USE_URL': True,
|
'UPLOADED_FILES_USE_URL': True,
|
||||||
|
|
||||||
|
# Browseable API
|
||||||
|
'HTML_SELECT_CUTOFF': 1000,
|
||||||
|
'HTML_SELECT_CUTOFF_TEXT': "More than {count} items...",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user