get_attribute method on fields

This commit is contained in:
Tom Christie 2014-10-16 20:45:36 +01:00
parent 7b666e982c
commit 32fd82ba0d
2 changed files with 8 additions and 7 deletions

View File

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

View File

@ -379,7 +379,11 @@ class Serializer(BaseSerializer):
fields = [field for field in self.fields.values() if not field.write_only]
for field in fields:
value = field.get_field_representation(instance)
attribute = field.get_attribute(instance)
if attribute is None:
value = None
else:
value = field.to_representation(attribute)
transform_method = getattr(self, 'transform_' + field.field_name, None)
if transform_method is not None:
value = transform_method(value)