mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-29 18:03:16 +03:00
Non-required fields with 'allow_null=True' should not imply a default value (#5639)
Ref #5518.
This commit is contained in:
parent
97f7a82b37
commit
905a5579df
|
@ -442,10 +442,10 @@ 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()
|
||||||
|
if self.allow_null:
|
||||||
|
return None
|
||||||
msg = (
|
msg = (
|
||||||
'Got {exc_type} when attempting to get a value for field '
|
'Got {exc_type} when attempting to get a value for field '
|
||||||
'`{field}` on serializer `{serializer}`.\nThe serializer '
|
'`{field}` on serializer `{serializer}`.\nThe serializer '
|
||||||
|
|
|
@ -372,6 +372,14 @@ class TestNotRequiredOutput:
|
||||||
serializer.save()
|
serializer.save()
|
||||||
assert serializer.data == {'included': 'abc'}
|
assert serializer.data == {'included': 'abc'}
|
||||||
|
|
||||||
|
def test_not_required_output_for_allow_null_field(self):
|
||||||
|
class ExampleSerializer(serializers.Serializer):
|
||||||
|
omitted = serializers.CharField(required=False, allow_null=True)
|
||||||
|
included = serializers.CharField()
|
||||||
|
|
||||||
|
serializer = ExampleSerializer({'included': 'abc'})
|
||||||
|
assert 'omitted' not in serializer.data
|
||||||
|
|
||||||
|
|
||||||
class TestDefaultOutput:
|
class TestDefaultOutput:
|
||||||
def setup(self):
|
def setup(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user