mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 21:10:13 +03:00
Default value will now be used when serializing if key or attribute is missing.
This commit is contained in:
parent
45e90c3398
commit
27e8f84855
|
@ -442,7 +442,9 @@ class Field(object):
|
||||||
try:
|
try:
|
||||||
return get_attribute(instance, self.source_attrs)
|
return get_attribute(instance, self.source_attrs)
|
||||||
except (KeyError, AttributeError) as exc:
|
except (KeyError, AttributeError) as exc:
|
||||||
if not self.required and self.default is empty:
|
if self.default is not empty:
|
||||||
|
return self.default
|
||||||
|
if not self.required:
|
||||||
raise SkipField()
|
raise SkipField()
|
||||||
msg = (
|
msg = (
|
||||||
'Got {exc_type} when attempting to get a value for field '
|
'Got {exc_type} when attempting to get a value for field '
|
||||||
|
|
|
@ -313,6 +313,7 @@ class TestNotRequiredOutput:
|
||||||
serializer.save()
|
serializer.save()
|
||||||
assert serializer.data == {'included': 'abc'}
|
assert serializer.data == {'included': 'abc'}
|
||||||
|
|
||||||
|
@pytest.mark.skipif(True, reason='Disabling pending removal')
|
||||||
def test_default_required_output_for_dict(self):
|
def test_default_required_output_for_dict(self):
|
||||||
"""
|
"""
|
||||||
'default="something"' should require dictionary key.
|
'default="something"' should require dictionary key.
|
||||||
|
@ -328,6 +329,7 @@ class TestNotRequiredOutput:
|
||||||
with pytest.raises(KeyError):
|
with pytest.raises(KeyError):
|
||||||
serializer.data
|
serializer.data
|
||||||
|
|
||||||
|
@pytest.mark.skipif(True, reason='Disabling pending removal')
|
||||||
def test_default_required_output_for_object(self):
|
def test_default_required_output_for_object(self):
|
||||||
"""
|
"""
|
||||||
'default="something"' should require object attribute.
|
'default="something"' should require object attribute.
|
||||||
|
@ -345,6 +347,44 @@ class TestNotRequiredOutput:
|
||||||
serializer.data
|
serializer.data
|
||||||
|
|
||||||
|
|
||||||
|
class TestDefaultOutput:
|
||||||
|
def setup(self):
|
||||||
|
class ExampleSerializer(serializers.Serializer):
|
||||||
|
has_default = serializers.CharField(default='abc')
|
||||||
|
no_default = serializers.CharField()
|
||||||
|
self.Serializer = ExampleSerializer
|
||||||
|
|
||||||
|
def test_default_used_for_dict(self):
|
||||||
|
"""
|
||||||
|
'default="something"' should be used if dictionary key is missing from input.
|
||||||
|
"""
|
||||||
|
serializer = self.Serializer({'no_default': 'def'})
|
||||||
|
assert serializer.data == {'has_default': 'abc', 'no_default': 'def'}
|
||||||
|
|
||||||
|
def test_default_used_for_object(self):
|
||||||
|
"""
|
||||||
|
'default="something"' should be used if object attribute is missing from input.
|
||||||
|
"""
|
||||||
|
instance = MockObject(no_default='def')
|
||||||
|
serializer = self.Serializer(instance)
|
||||||
|
assert serializer.data == {'has_default': 'abc', 'no_default': 'def'}
|
||||||
|
|
||||||
|
def test_default_not_used_when_in_dict(self):
|
||||||
|
"""
|
||||||
|
'default="something"' should not be used if dictionary key is present in input.
|
||||||
|
"""
|
||||||
|
serializer = self.Serializer({'has_default': 'ghi', 'no_default': 'def'})
|
||||||
|
assert serializer.data == {'has_default': 'ghi', 'no_default': 'def'}
|
||||||
|
|
||||||
|
def test_default_not_used_when_in_object(self):
|
||||||
|
"""
|
||||||
|
'default="something"' should not be used if object attribute is present in input.
|
||||||
|
"""
|
||||||
|
instance = MockObject(has_default='ghi', no_default='def')
|
||||||
|
serializer = self.Serializer(instance)
|
||||||
|
assert serializer.data == {'has_default': 'ghi', 'no_default': 'def'}
|
||||||
|
|
||||||
|
|
||||||
class TestCacheSerializerData:
|
class TestCacheSerializerData:
|
||||||
def test_cache_serializer_data(self):
|
def test_cache_serializer_data(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user