mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 05:20:12 +03:00
add required argument in get_attribute
This commit is contained in:
parent
4d433b6513
commit
6ee591b78c
|
@ -48,7 +48,7 @@ def is_simple_callable(obj):
|
||||||
return len_args <= len_defaults
|
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)`,
|
Similar to Python's built in `getattr(instance, attr)`,
|
||||||
but takes a list of nested attributes, instead of a single attribute.
|
but takes a list of nested attributes, instead of a single attribute.
|
||||||
|
@ -69,6 +69,8 @@ def get_attribute(instance, attrs):
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
return None
|
return None
|
||||||
except AttributeError as exc:
|
except AttributeError as exc:
|
||||||
|
if not required:
|
||||||
|
return None
|
||||||
raise exc
|
raise exc
|
||||||
if is_simple_callable(instance):
|
if is_simple_callable(instance):
|
||||||
instance = instance()
|
instance = instance()
|
||||||
|
@ -277,7 +279,7 @@ class Field(object):
|
||||||
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.
|
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):
|
def get_default(self):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user