mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
docs: add paragraph on transform_fieldname methods
This commit is contained in:
parent
dc650f77b5
commit
42bbf6907e
|
@ -63,6 +63,21 @@ At this point we've translated the model instance into Python native datatypes.
|
||||||
json
|
json
|
||||||
# '{"email": "leila@example.com", "content": "foo bar", "created": "2012-08-22T16:20:09.822"}'
|
# '{"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
|
## Deserializing objects
|
||||||
|
|
||||||
Deserialization is similar. First we parse a stream into Python native datatypes...
|
Deserialization is similar. First we parse a stream into Python native datatypes...
|
||||||
|
|
Loading…
Reference in New Issue
Block a user