Simplify serialization slightly

This commit is contained in:
Tom Christie 2014-09-26 14:32:44 +01:00
parent 8be4496586
commit 6090144608
2 changed files with 8 additions and 6 deletions

View File

@ -202,12 +202,15 @@ class Field(object):
return self.default_empty_html if (ret == '') else ret
return dictionary.get(self.field_name, empty)
def get_attribute(self, instance):
def get_field_representation(self, instance):
"""
Given the *outgoing* object instance, return the value for this field
that should be returned as a primative value.
Given the outgoing object instance, return the primative value
that should be used for this field.
"""
return get_attribute(instance, self.source_attrs)
attribute = get_attribute(instance, self.source_attrs)
if attribute is None:
return None
return self.to_representation(attribute)
def get_default(self):
"""

View File

@ -268,8 +268,7 @@ class Serializer(BaseSerializer):
fields = [field for field in self.fields.values() if not field.write_only]
for field in fields:
native_value = field.get_attribute(instance)
ret[field.field_name] = field.to_representation(native_value)
ret[field.field_name] = field.get_field_representation(instance)
return ret