def get_attribute(self, instance): if self.use_pk_only_optimization() and self.source_attrs: # Optimized case, return a mock object only containing the # pk attribute try: instance = get_attribute(instance, self.source_attrs[:-1]) value = getattr(instance, self.source_attrs[-1]) return PKOnlyObject(pk=value) except AttributeError: pass # Standard case, return the object instance return get_attribute(instance, self.source_attrs)
def get_attribute(self, instance): """ Given the *outgoing* object instance, return the primitive value that should be used for this field. :param instance: object, from which value will have taken. """ try: return get_attribute(instance, self.source_attrs) except (KeyError, AttributeError) as exc: if not self.required and self.default is empty: raise SkipField() msg = ( 'Got {exc_type} when attempting to get a value for field ' '`{field}` on serializer `{serializer}`.\nThe serializer ' 'field might be named incorrectly and not match ' 'any attribute or key on the `{instance}` instance.\n' 'Original exception text was: {exc}.'.format( exc_type=type(exc).__name__, field=self.field_name, serializer=self.parent.__class__.__name__, instance=instance.__class__.__name__, exc=exc ) ) raise type(exc)(msg)
def get_attribute(self, instance): if self.use_pk_only_optimization() and self.source_attrs: # Optimized case, return a mock object only containing the # pk attribute try: instance = get_attribute(instance, self.source_attrs[:-1]) value = instance.serializable_value(self.source_attrs[-1]) if is_simple_callable(value): # Handle edge case where the relationship `source` argument # points to a `get_relationship()` method on the model value = value().pk return relations.PKOnlyObject(pk=value) except AttributeError: pass # Standard case, return the object instance. return get_attribute(instance, self.source_attrs)
def get_attribute(self, instance): # Can't have any relationships if not created if hasattr(instance, 'pk') and instance.pk is None: return [] relationship = get_attribute(instance, self.source_attrs) if hasattr(relationship, 'all'): return relationship.all() return relationship
def get_attribute(self, instance): try: return get_attribute(instance, self.source_attrs) except (KeyError, AttributeError) as exc: if not self.required and self.default is empty: raise SkipField() msg = ( 'Got {exc_type} when attempting to get a value for field ' '`{field}` on serializer `{serializer}`.\nThe serializer ' 'field might be named incorrectly and not match ' 'any attribute or key on the `{instance}` instance.\n' 'Original exception text was: {exc}.'.format( exc_type=type(exc).__name__, field=self.field_name, serializer=self.parent.__class__.__name__, instance=instance.__class__.__name__, exc=exc ) ) raise type(exc)(msg)
def get_attribute(self, instance): """ Given the *outgoing* object instance, return the primitive value that should be used for this field. :param instance: object, from which value will have taken. """ try: return get_attribute(instance, self.source_attrs) except (KeyError, AttributeError) as exc: if not self.required and self.default is empty: raise SkipField() msg = ('Got {exc_type} when attempting to get a value for field ' '`{field}` on serializer `{serializer}`.\nThe serializer ' 'field might be named incorrectly and not match ' 'any attribute or key on the `{instance}` instance.\n' 'Original exception text was: {exc}.'.format( exc_type=type(exc).__name__, field=self.field_name, serializer=self.parent.__class__.__name__, instance=instance.__class__.__name__, exc=exc)) raise type(exc)(msg)
def test_get_attribute_failed(instance, attrs, exc_cls): with pytest.raises(exc_cls): get_attribute(instance, attrs)
def test_get_attribute(instance, attrs, expected): assert get_attribute(instance, attrs) == expected
def get_attribute(self, instance): object_state = instance._sa_instance_state if object_state.transient or object_state.pending: return [] return get_attribute(instance, self.source_attrs)