mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-25 19:14:01 +03:00
Serializer defaults should not be included in partial updates. (#4346)
Serializer default values should not be included in partial updates
This commit is contained in:
parent
296e47a9f8
commit
e37619f741
|
@ -49,7 +49,9 @@ Defaults to `False`
|
|||
|
||||
### `default`
|
||||
|
||||
If set, this gives the default value that will be used for the field if no input value 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 no input value is supplied. If not set the default behaviour is to not populate the attribute at all.
|
||||
|
||||
The `default` is not applied during partial update operations. In the partial update case only fields that are provided in the incoming data will have a validated value returned.
|
||||
|
||||
May be set to a function or other callable, in which case the value will be evaluated each time it is used. When called, it will receive no arguments. If the callable has a `set_context` method, that will be called each time before getting the value with the field instance as only argument. This works the same way as for [validators](validators.md#using-set_context).
|
||||
|
||||
|
|
|
@ -435,7 +435,8 @@ class Field(object):
|
|||
return `empty`, indicating that no value should be set in the
|
||||
validated data for this field.
|
||||
"""
|
||||
if self.default is empty:
|
||||
if self.default is empty or getattr(self.root, 'partial', False):
|
||||
# No default, or this is a partial update.
|
||||
raise SkipField()
|
||||
if callable(self.default):
|
||||
if hasattr(self.default, 'set_context'):
|
||||
|
|
|
@ -309,3 +309,31 @@ class TestCacheSerializerData:
|
|||
pickled = pickle.dumps(serializer.data)
|
||||
data = pickle.loads(pickled)
|
||||
assert data == {'field1': 'a', 'field2': 'b'}
|
||||
|
||||
|
||||
class TestDefaultInclusions:
|
||||
def setup(self):
|
||||
class ExampleSerializer(serializers.Serializer):
|
||||
char = serializers.CharField(read_only=True, default='abc')
|
||||
integer = serializers.IntegerField()
|
||||
self.Serializer = ExampleSerializer
|
||||
|
||||
def test_default_should_included_on_create(self):
|
||||
serializer = self.Serializer(data={'integer': 456})
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {'char': 'abc', 'integer': 456}
|
||||
assert serializer.errors == {}
|
||||
|
||||
def test_default_should_be_included_on_update(self):
|
||||
instance = MockObject(char='def', integer=123)
|
||||
serializer = self.Serializer(instance, data={'integer': 456})
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {'char': 'abc', 'integer': 456}
|
||||
assert serializer.errors == {}
|
||||
|
||||
def test_default_should_not_be_included_on_partial_update(self):
|
||||
instance = MockObject(char='def', integer=123)
|
||||
serializer = self.Serializer(instance, data={'integer': 456}, partial=True)
|
||||
assert serializer.is_valid()
|
||||
assert serializer.validated_data == {'integer': 456}
|
||||
assert serializer.errors == {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user