diff options
| author | Tom Christie | 2014-11-03 16:42:00 +0000 | 
|---|---|---|
| committer | Tom Christie | 2014-11-03 16:42:00 +0000 | 
| commit | 1925d465ae0c3558d6919e56ee753602ac61119f (patch) | |
| tree | 433af0b00b8bb04ba1661cbc3dd088b1613c7dc9 /docs | |
| parent | 5d7b8356082e7c70f2723d1773c13125a4608dcc (diff) | |
| download | django-rest-framework-1925d465ae0c3558d6919e56ee753602ac61119f.tar.bz2 | |
Call out 'get_value' and 'get_attribute'
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/topics/3.0-announcement.md | 21 | 
1 files changed, 20 insertions, 1 deletions
| diff --git a/docs/topics/3.0-announcement.md b/docs/topics/3.0-announcement.md index b30643c0..78abc5ee 100644 --- a/docs/topics/3.0-announcement.md +++ b/docs/topics/3.0-announcement.md @@ -562,7 +562,26 @@ The `MultipleChoiceField` class has been added. This field acts like `ChoiceFiel  The `from_native(self, value)` and `to_native(self, data)` method names have been replaced with the more obviously named `to_internal_value(self, data)` and `to_representation(self, value)`. -The `field_from_native()` and `field_to_native()` methods are removed. +The `field_from_native()` and `field_to_native()` methods are removed. Previously you could use these methods if you wanted to customise the behaviour in a way that did not simply lookup the field value from the object. For example... + +    def field_to_native(self, obj, field_name): +        """A custom read-only field that returns the class name.""" +        return obj.__class__.__name__ + +Now if you need to access the entire object you'll instead need to override one or both of the following: + +* Use `get_attribute` to modify the attribute value passed to `to_representation()`. +* Use `get_value` to modify the data value passed `to_internal_value()`. + +For example: + +    def get_attribute(self, obj): +        # Pass the entire object through to `to_representation()`, +        # instead of the standard attribute lookup. +        return obj + +    def to_representation(self, value): +        return value.__class__.__name__  #### Explicit `queryset` required on relational fields. | 
