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

Method to_representation

rest_framework/serializers.py:530–554  ·  view source on GitHub ↗

Object instance -> Dict of primitive datatypes.

(self, instance)

Source from the content-addressed store, hash-verified

528 return ret
529
530 def to_representation(self, instance):
531 """
532 Object instance -> Dict of primitive datatypes.
533 """
534 ret = {}
535 fields = self._readable_fields
536
537 for field in fields:
538 try:
539 attribute = field.get_attribute(instance)
540 except SkipField:
541 continue
542
543 # We skip `to_representation` for `None` values so that fields do
544 # not have to explicitly deal with that case.
545 #
546 # For related fields with `use_pk_only_optimization` we need to
547 # resolve the pk value.
548 check_for_none = attribute.pk if isinstance(attribute, PKOnlyObject) else attribute
549 if check_for_none is None:
550 ret[field.field_name] = None
551 else:
552 ret[field.field_name] = field.to_representation(attribute)
553
554 return ret
555
556 def validate(self, attrs):
557 return attrs

Callers 1

to_representationMethod · 0.45

Calls 1

get_attributeMethod · 0.45

Tested by

no test coverage detected