mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-08 06:14:47 +03:00
fixed PrimaryKeyRelatedField(required=False, ...) with missing data
This commit is contained in:
parent
85e57afc20
commit
3500b21038
|
@ -62,7 +62,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=True):
|
||||||
"""
|
"""
|
||||||
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.
|
||||||
|
@ -80,6 +80,12 @@ def get_attribute(instance, attrs):
|
||||||
instance = getattr(instance, attr)
|
instance = getattr(instance, attr)
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
return None
|
return None
|
||||||
|
# RelatedObjectDoesNotExist inherits from both ObjectDoesNotExist and
|
||||||
|
# AttributeError, so ObjectDoesNotExist has to come first
|
||||||
|
except (AttributeError, KeyError):
|
||||||
|
if not required:
|
||||||
|
raise SkipField
|
||||||
|
raise
|
||||||
if is_simple_callable(instance):
|
if is_simple_callable(instance):
|
||||||
try:
|
try:
|
||||||
instance = instance()
|
instance = instance()
|
||||||
|
|
|
@ -154,7 +154,7 @@ class RelatedField(Field):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Standard case, return the object instance.
|
# Standard case, return the object instance.
|
||||||
return get_attribute(instance, self.source_attrs)
|
return get_attribute(instance, self.source_attrs, self.required)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def choices(self):
|
def choices(self):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user