diff options
| author | Craig de Stigter | 2013-10-03 12:38:42 +1300 |
|---|---|---|
| committer | Craig de Stigter | 2013-10-03 12:38:42 +1300 |
| commit | 42bbf6907e041d6abe773854b9aaa53eded82f4e (patch) | |
| tree | 76c85a06658c35ed04eb538c281a6a6e46d32cf3 /docs/api-guide/serializers.md | |
| parent | dc650f77b5f377bbfab3da66455feb57b195a1da (diff) | |
| download | django-rest-framework-42bbf6907e041d6abe773854b9aaa53eded82f4e.tar.bz2 | |
docs: add paragraph on transform_fieldname methods
Diffstat (limited to 'docs/api-guide/serializers.md')
| -rw-r--r-- | docs/api-guide/serializers.md | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/docs/api-guide/serializers.md b/docs/api-guide/serializers.md index bbc8d019..2d3e999f 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -63,6 +63,21 @@ At this point we've translated the model instance into Python native datatypes. json # '{"email": "leila@example.com", "content": "foo bar", "created": "2012-08-22T16:20:09.822"}' +### Customizing field representation + +Sometimes when serializing objects, you may not want to represent everything exactly the way it is in your model. + +If you need to customize the serialized value of a particular field, you can do this by creating a `transform_<fieldname>` method. For example if you needed to render some markdown from a text field: + + description = serializers.TextField() + description_html = serializers.TextField(source='description', read_only=True) + + def transform_description_html(self, obj, value): + from django.contrib.markup.templatetags.markup import markdown + return markdown(value) + +These methods are essentially the reverse of `validate_<fieldname>` (see *Validation* below.) + ## Deserializing objects Deserialization is similar. First we parse a stream into Python native datatypes... |
