mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-13 18:11:05 +03:00
Fields with 'allow_null=True' should imply a default serialization value (#5518)
* Add test for dotted source + allow_null * Field 'allow_null' implies 'default=None' * Field 'allow_null' provides serialization default
This commit is contained in:
parent
1f693c331e
commit
5009aeff18
|
@ -442,6 +442,8 @@ class Field(object):
|
||||||
except (KeyError, AttributeError) as exc:
|
except (KeyError, AttributeError) as exc:
|
||||||
if self.default is not empty:
|
if self.default is not empty:
|
||||||
return self.get_default()
|
return self.get_default()
|
||||||
|
if self.allow_null:
|
||||||
|
return None
|
||||||
if not self.required:
|
if not self.required:
|
||||||
raise SkipField()
|
raise SkipField()
|
||||||
msg = (
|
msg = (
|
||||||
|
|
|
@ -449,6 +449,14 @@ class TestDefaultOutput:
|
||||||
assert Serializer({'nested': {'a': '3', 'b': {}}}).data == {'nested': {'a': '3', 'c': '2'}}
|
assert Serializer({'nested': {'a': '3', 'b': {}}}).data == {'nested': {'a': '3', 'c': '2'}}
|
||||||
assert Serializer({'nested': {'a': '3', 'b': {'c': '4'}}}).data == {'nested': {'a': '3', 'c': '4'}}
|
assert Serializer({'nested': {'a': '3', 'b': {'c': '4'}}}).data == {'nested': {'a': '3', 'c': '4'}}
|
||||||
|
|
||||||
|
def test_default_for_allow_null(self):
|
||||||
|
# allow_null=True should imply default=None
|
||||||
|
class Serializer(serializers.Serializer):
|
||||||
|
foo = serializers.CharField()
|
||||||
|
bar = serializers.CharField(source='foo.bar', allow_null=True)
|
||||||
|
|
||||||
|
assert Serializer({'foo': None}).data == {'foo': None, 'bar': None}
|
||||||
|
|
||||||
|
|
||||||
class TestCacheSerializerData:
|
class TestCacheSerializerData:
|
||||||
def test_cache_serializer_data(self):
|
def test_cache_serializer_data(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user