Bring check for null fk to BaseSerializer.to_representation

This commit is contained in:
Carlton Gibson 2016-03-13 20:39:19 +01:00
parent cbb8d8d254
commit 2ef74cfa61
2 changed files with 7 additions and 5 deletions

View File

@ -778,8 +778,6 @@ class UUIDField(Field):
return data
def to_representation(self, value):
if value is None:
return None
if self.uuid_format == 'hex_verbose':
return str(value)
else:

View File

@ -464,9 +464,13 @@ class Serializer(BaseSerializer):
except SkipField:
continue
if attribute is None:
# We skip `to_representation` for `None` values so that
# fields do not have to explicitly deal with that case.
# We skip `to_representation` for `None` values so that fields do
# not have to explicitly deal with that case.
#
# For related fields with `use_pk_only_optimization` we need to
# resolve the pk value.
check_for_none = attribute.pk if isinstance(attribute, PKOnlyObject) else attribute
if check_for_none is None:
ret[field.field_name] = None
else:
ret[field.field_name] = field.to_representation(attribute)