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

Method get_attribute

rest_framework/fields.py:431–470  ·  view source on GitHub ↗

Given the *outgoing* object instance, return the primitive value that should be used for this field.

(self, instance)

Source from the content-addressed store, hash-verified

429 return dictionary.get(self.field_name, empty)
430
431 def get_attribute(self, instance):
432 """
433 Given the *outgoing* object instance, return the primitive value
434 that should be used for this field.
435 """
436 try:
437 return get_attribute(instance, self.source_attrs)
438 except BuiltinSignatureError as exc:
439 msg = (
440 'Field source for `{serializer}.{field}` maps to a built-in '
441 'function type and is invalid. Define a property or method on '
442 'the `{instance}` instance that wraps the call to the built-in '
443 'function.'.format(
444 serializer=self.parent.__class__.__name__,
445 field=self.field_name,
446 instance=instance.__class__.__name__,
447 )
448 )
449 raise type(exc)(msg)
450 except (KeyError, AttributeError) as exc:
451 if self.default is not empty:
452 return self.get_default()
453 if self.allow_null:
454 return None
455 if not self.required:
456 raise SkipField()
457 msg = (
458 'Got {exc_type} when attempting to get a value for field '
459 '`{field}` on serializer `{serializer}`.\nThe serializer '
460 'field might be named incorrectly and not match '
461 'any attribute or key on the `{instance}` instance.\n'
462 'Original exception text was: {exc}.'.format(
463 exc_type=type(exc).__name__,
464 field=self.field_name,
465 serializer=self.parent.__class__.__name__,
466 instance=instance.__class__.__name__,
467 exc=exc
468 )
469 )
470 raise type(exc)(msg)
471
472 def get_default(self):
473 """

Callers 1

to_representationMethod · 0.45

Calls 3

get_defaultMethod · 0.95
get_attributeFunction · 0.85
SkipFieldClass · 0.85

Tested by

no test coverage detected