Skip to content

Commit

Permalink
Bring check for null fk to BaseSerializer.to_representation
Browse files Browse the repository at this point in the history
  • Loading branch information
carltongibson committed Mar 13, 2016
1 parent cbb8d8d commit 2ef74cf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 0 additions & 2 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,6 @@ def to_internal_value(self, data):
return data

def to_representation(self, value):
if value is None:
return None
if self.uuid_format == 'hex_verbose':
return str(value)
else:
Expand Down
10 changes: 7 additions & 3 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,13 @@ def to_representation(self, instance):
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)
Expand Down

0 comments on commit 2ef74cf

Please sign in to comment.