diff options
| author | Carlton Gibson | 2013-10-09 01:11:46 -0700 |
|---|---|---|
| committer | Carlton Gibson | 2013-10-09 01:11:46 -0700 |
| commit | 0bbc775b950d4ff7d3caef4fda719642ad792075 (patch) | |
| tree | 7d1d4b2e291c8cf6d490f832e716d4e1e5752f00 /docs/api-guide/serializers.md | |
| parent | 761e66ffdda97833b15b7cbcea2b143988aabe29 (diff) | |
| parent | 42bbf6907e041d6abe773854b9aaa53eded82f4e (diff) | |
| download | django-rest-framework-0bbc775b950d4ff7d3caef4fda719642ad792075.tar.bz2 | |
Merge pull request #1071 from craigds/field-transform-methods
Feature: add transform_<fieldname> methods to serializers
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 6b91aa76..4c3fb9d3 100644 --- a/docs/api-guide/serializers.md +++ b/docs/api-guide/serializers.md @@ -67,6 +67,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... |
