MCPcopy Index your code
hub / github.com/encode/django-rest-framework / get_attribute

Method get_attribute

rest_framework/relations.py:172–189  ·  view source on GitHub ↗
(self, instance)

Source from the content-addressed store, hash-verified

170 return False
171
172 def get_attribute(self, instance):
173 if self.use_pk_only_optimization() and self.source_attrs:
174 # Optimized case, return a mock object only containing the pk attribute.
175 with contextlib.suppress(AttributeError):
176 attribute_instance = get_attribute(instance, self.source_attrs[:-1])
177 value = attribute_instance.serializable_value(self.source_attrs[-1])
178 if is_simple_callable(value):
179 # Handle edge case where the relationship `source` argument
180 # points to a `get_relationship()` method on the model.
181 value = value()
182
183 # Handle edge case where relationship `source` argument points
184 # to an instance instead of a pk (e.g., a `@property`).
185 value = getattr(value, 'pk', value)
186
187 return PKOnlyObject(pk=value)
188 # Standard case, return the object instance.
189 return super().get_attribute(instance)
190
191 def get_choices(self, cutoff=None):
192 queryset = self.get_queryset()

Callers

nothing calls this directly

Calls 4

get_attributeFunction · 0.90
is_simple_callableFunction · 0.90
PKOnlyObjectClass · 0.85

Tested by

no test coverage detected