mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-06 05:20:12 +03:00
fix relations get_attribute
This commit is contained in:
parent
6ee591b78c
commit
0f41623c54
|
@ -85,7 +85,7 @@ class RelatedField(Field):
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
def get_iterable(self, instance, source_attrs):
|
def get_iterable(self, instance, source_attrs):
|
||||||
relationship = get_attribute(instance, source_attrs)
|
relationship = get_attribute(instance, source_attrs, self.required)
|
||||||
return relationship.all() if (hasattr(relationship, 'all')) else relationship
|
return relationship.all() if (hasattr(relationship, 'all')) else relationship
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -134,10 +134,10 @@ class PrimaryKeyRelatedField(RelatedField):
|
||||||
# the related object. We return this directly instead of returning the
|
# the related object. We return this directly instead of returning the
|
||||||
# object itself, which would require a database lookup.
|
# object itself, which would require a database lookup.
|
||||||
try:
|
try:
|
||||||
instance = get_attribute(instance, self.source_attrs[:-1])
|
instance = get_attribute(instance, self.source_attrs[:-1], self.required)
|
||||||
return PKOnlyObject(pk=instance.serializable_value(self.source_attrs[-1]))
|
return PKOnlyObject(pk=instance.serializable_value(self.source_attrs[-1]))
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return get_attribute(instance, self.source_attrs)
|
return get_attribute(instance, self.source_attrs, self.required)
|
||||||
|
|
||||||
def get_iterable(self, instance, source_attrs):
|
def get_iterable(self, instance, source_attrs):
|
||||||
# For consistency with `get_attribute` we're using `serializable_value()`
|
# For consistency with `get_attribute` we're using `serializable_value()`
|
||||||
|
@ -349,7 +349,7 @@ class ManyRelatedField(Field):
|
||||||
]
|
]
|
||||||
|
|
||||||
def get_attribute(self, instance):
|
def get_attribute(self, instance):
|
||||||
return self.child_relation.get_iterable(instance, self.source_attrs)
|
return self.child_relation.get_iterable(instance, self.source_attrs, self.required)
|
||||||
|
|
||||||
def to_representation(self, iterable):
|
def to_representation(self, iterable):
|
||||||
return [
|
return [
|
||||||
|
|
Loading…
Reference in New Issue
Block a user