From 03c251ff49b8c49c44856de7912fdb5b0bf3d30b Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Tue, 19 Dec 2017 10:24:09 +0100 Subject: [PATCH] Add example to to_representation docs Closes #5425 as per https://github.com/encode/django-rest-framework/issues/5425#issuecomment-341063819 --- docs/api-guide/serializers.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index 89196949d..85682838b 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -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.