mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-03 05:04:31 +03:00
Call out 'get_value' and 'get_attribute'
This commit is contained in:
parent
5d7b835608
commit
1925d465ae
|
@ -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.
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user