mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-09 08:00:52 +03:00
Serializer field 'default' argument may be a callable
This commit is contained in:
parent
01e80ff9f7
commit
b15a6ccef2
|
@ -41,7 +41,9 @@ Defaults to `True`.
|
||||||
|
|
||||||
### `default`
|
### `default`
|
||||||
|
|
||||||
If set, this gives the default value that will be used for the field if none is supplied. If not set the default behavior is to not populate the attribute at all.
|
If set, this gives the default value that will be used for the field if none is supplied. If not set the default behavior is to not populate the attribute at all.
|
||||||
|
|
||||||
|
May be set to a function or other callable, in which case the value will be evaluated each time it is used.
|
||||||
|
|
||||||
### `validators`
|
### `validators`
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,7 @@ You can determine your currently installed version using `pip freeze`:
|
||||||
### Master
|
### Master
|
||||||
|
|
||||||
* Added `get_url` hook to `HyperlinkedIdentityField`.
|
* Added `get_url` hook to `HyperlinkedIdentityField`.
|
||||||
|
* Serializer field `default` argument may be a callable.
|
||||||
* Bugfix: The `lookup_field` option on `HyperlinkedIdentityField` should apply by default to the url field on the serializer.
|
* Bugfix: The `lookup_field` option on `HyperlinkedIdentityField` should apply by default to the url field on the serializer.
|
||||||
* Bugfix: `HyperlinkedIdentityField` should continue to support `pk_url_kwarg`, `slug_url_kwarg`, `slug_field`, in a pending deprecation state.
|
* Bugfix: `HyperlinkedIdentityField` should continue to support `pk_url_kwarg`, `slug_url_kwarg`, `slug_field`, in a pending deprecation state.
|
||||||
* Bugfix: Ensure we always return 404 instead of 500 if a lookup field cannot be converted to the correct lookup type. (Eg non-numeric `AutoInteger` pk lookup)
|
* Bugfix: Ensure we always return 404 instead of 500 if a lookup field cannot be converted to the correct lookup type. (Eg non-numeric `AutoInteger` pk lookup)
|
||||||
|
|
|
@ -250,9 +250,6 @@ class WritableField(Field):
|
||||||
self.validators = self.default_validators + validators
|
self.validators = self.default_validators + validators
|
||||||
self.default = default if default is not None else self.default
|
self.default = default if default is not None else self.default
|
||||||
|
|
||||||
if is_simple_callable(self.default):
|
|
||||||
self.default = self.default()
|
|
||||||
|
|
||||||
# Widgets are ony used for HTML forms.
|
# Widgets are ony used for HTML forms.
|
||||||
widget = widget or self.widget
|
widget = widget or self.widget
|
||||||
if isinstance(widget, type):
|
if isinstance(widget, type):
|
||||||
|
@ -298,7 +295,10 @@ class WritableField(Field):
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if self.default is not None and not self.partial:
|
if self.default is not None and not self.partial:
|
||||||
# Note: partial updates shouldn't set defaults
|
# Note: partial updates shouldn't set defaults
|
||||||
native = self.default
|
if is_simple_callable(self.default):
|
||||||
|
native = self.default()
|
||||||
|
else:
|
||||||
|
native = self.default
|
||||||
else:
|
else:
|
||||||
if self.required:
|
if self.required:
|
||||||
raise ValidationError(self.error_messages['required'])
|
raise ValidationError(self.error_messages['required'])
|
||||||
|
|
|
@ -852,3 +852,17 @@ class FieldMetadata(TestCase):
|
||||||
def test_label(self):
|
def test_label(self):
|
||||||
for field in (self.required_field, self.optional_field):
|
for field in (self.required_field, self.optional_field):
|
||||||
self.assertEqual(field.metadata()['label'], field.label)
|
self.assertEqual(field.metadata()['label'], field.label)
|
||||||
|
|
||||||
|
|
||||||
|
class FieldCallableDefault(TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.simple_callable = lambda: 'foo bar'
|
||||||
|
|
||||||
|
def test_default_can_be_simple_callable(self):
|
||||||
|
"""
|
||||||
|
Ensure that the 'default' argument can also be a simple callable.
|
||||||
|
"""
|
||||||
|
field = serializers.WritableField(default=self.simple_callable)
|
||||||
|
into = {}
|
||||||
|
field.field_from_native({}, {}, 'field', into)
|
||||||
|
self.assertEquals(into, {'field': 'foo bar'})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user