From 6ee591b78cc984e2270227cd70161eb8dd300dc1 Mon Sep 17 00:00:00 2001 From: Catstyle Date: Tue, 9 Dec 2014 18:58:25 +0800 Subject: [PATCH] add required argument in get_attribute --- rest_framework/fields.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 8aec5c2b2..70b71666a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -48,7 +48,7 @@ def is_simple_callable(obj): return len_args <= len_defaults -def get_attribute(instance, attrs): +def get_attribute(instance, attrs, required): """ Similar to Python's built in `getattr(instance, attr)`, but takes a list of nested attributes, instead of a single attribute. @@ -69,6 +69,8 @@ def get_attribute(instance, attrs): except ObjectDoesNotExist: return None except AttributeError as exc: + if not required: + return None raise exc if is_simple_callable(instance): instance = instance() @@ -277,7 +279,7 @@ class Field(object): Given the *outgoing* object instance, return the primitive value that should be used for this field. """ - return get_attribute(instance, self.source_attrs) + return get_attribute(instance, self.source_attrs, self.required) def get_default(self): """