Non-required fields with 'allow_null=True' should not imply a default value (#5639)

Ref #5518.
This commit is contained in:
Rômulo Oliveira 2017-12-01 06:54:25 -02:00 committed by Carlton Gibson
parent 97f7a82b37
commit 905a5579df
2 changed files with 10 additions and 2 deletions

View File

@ -442,10 +442,10 @@ class Field(object):
except (KeyError, AttributeError) as exc:
if self.default is not empty:
return self.get_default()
if self.allow_null:
return None
if not self.required:
raise SkipField()
if self.allow_null:
return None
msg = (
'Got {exc_type} when attempting to get a value for field '
'`{field}` on serializer `{serializer}`.\nThe serializer '

View File

@ -372,6 +372,14 @@ class TestNotRequiredOutput:
serializer.save()
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:
def setup(self):