From 609014460861fdfe82054551790d6439292dde7b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 26 Sep 2014 14:32:44 +0100 Subject: [PATCH] Simplify serialization slightly --- rest_framework/fields.py | 11 +++++++---- rest_framework/serializers.py | 3 +-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index cf42d36cb..4c49aabaa 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -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): """ diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index fa2e8fb10..080b958d5 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -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