[WIP/RFC] use field default value if not allow_null

This is meant to be used with FK remote field lookups (`source =
'foo.bar'`), where `foo` is a model and `bar` is `None` there.

     serializers.CharField(source='foo.bar', default='default')
This commit is contained in:
Daniel Hahler 2018-02-07 20:02:41 +01:00
parent c456b3c510
commit 929937693c

View File

@ -499,6 +499,13 @@ class Serializer(BaseSerializer):
# resolve the pk value.
check_for_none = attribute.pk if isinstance(attribute, PKOnlyObject) else attribute
if check_for_none is None:
if not field.allow_null:
try:
ret[field.field_name] = field.get_default()
except SkipField:
pass
else:
continue
ret[field.field_name] = None
else:
ret[field.field_name] = field.to_representation(attribute)