Tweaks to display nested data in empty serializers

This commit is contained in:
Tom Christie 2013-08-23 14:06:14 +01:00
parent 315435d00a
commit e03854ba6a
2 changed files with 9 additions and 4 deletions

View File

@ -134,9 +134,9 @@ class RelatedField(WritableField):
value = obj
for component in source.split('.'):
value = get_component(value, component)
if value is None:
break
value = get_component(value, component)
except ObjectDoesNotExist:
return None
@ -567,8 +567,13 @@ class HyperlinkedIdentityField(Field):
May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
attributes are not configured to correctly match the URL conf.
"""
lookup_field = getattr(obj, self.lookup_field)
lookup_field = getattr(obj, self.lookup_field, None)
kwargs = {self.lookup_field: lookup_field}
# Handle unsaved object case
if lookup_field is None:
return None
try:
return reverse(view_name, kwargs=kwargs, request=request, format=format)
except NoReverseMatch:

View File

@ -338,9 +338,9 @@ class BaseSerializer(WritableField):
value = obj
for component in source.split('.'):
value = get_component(value, component)
if value is None:
break
return self.to_native(None)
value = get_component(value, component)
except ObjectDoesNotExist:
return None