Add example to to_representation docs (#5682)

Closes #5425 as per https://github.com/encode/django-rest-framework/issues/5425#issuecomment-341063819
This commit is contained in:
Carlton Gibson 2017-12-19 12:06:24 +01:00 committed by GitHub
parent 43c2c91dde
commit b3a0b271cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1007,6 +1007,14 @@ The signatures for these methods are as follows:
Takes the object instance that requires serialization, and should return a primitive representation. Typically this means returning a structure of built-in Python datatypes. The exact types that can be handled will depend on the render classes you have configured for your API.
May be overridden in order modify the representation style. For example:
def to_representation(self, instance):
"""Convert `username` to lowercase."""
ret = super().to_representation(instance)
ret['username'] = ret['username'].lower()
return ret
#### ``.to_internal_value(self, data)``
Takes the unvalidated incoming data as input and should return the validated data that will be made available as `serializer.validated_data`. The return value will also be passed to the `.create()` or `.update()` methods if `.save()` is called on the serializer class.