mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-02-16 19:41:06 +03:00
Update serializer docs to use correct param name (#6995)
Avoids a pylint warning ".to_representation: Parameters differ from overridden 'to_representation' method" if people copy these examples.
This commit is contained in:
parent
0fd72f17ee
commit
3aa1089a6a
|
@ -887,10 +887,10 @@ To implement a read-only serializer using the `BaseSerializer` class, we just ne
|
||||||
It's simple to create a read-only serializer for converting `HighScore` instances into primitive data types.
|
It's simple to create a read-only serializer for converting `HighScore` instances into primitive data types.
|
||||||
|
|
||||||
class HighScoreSerializer(serializers.BaseSerializer):
|
class HighScoreSerializer(serializers.BaseSerializer):
|
||||||
def to_representation(self, obj):
|
def to_representation(self, instance):
|
||||||
return {
|
return {
|
||||||
'score': obj.score,
|
'score': instance.score,
|
||||||
'player_name': obj.player_name
|
'player_name': instance.player_name
|
||||||
}
|
}
|
||||||
|
|
||||||
We can now use this class to serialize single `HighScore` instances:
|
We can now use this class to serialize single `HighScore` instances:
|
||||||
|
@ -945,10 +945,10 @@ Here's a complete example of our previous `HighScoreSerializer`, that's been upd
|
||||||
'player_name': player_name
|
'player_name': player_name
|
||||||
}
|
}
|
||||||
|
|
||||||
def to_representation(self, obj):
|
def to_representation(self, instance):
|
||||||
return {
|
return {
|
||||||
'score': obj.score,
|
'score': instance.score,
|
||||||
'player_name': obj.player_name
|
'player_name': instance.player_name
|
||||||
}
|
}
|
||||||
|
|
||||||
def create(self, validated_data):
|
def create(self, validated_data):
|
||||||
|
@ -965,10 +965,10 @@ The following class is an example of a generic serializer that can handle coerci
|
||||||
A read-only serializer that coerces arbitrary complex objects
|
A read-only serializer that coerces arbitrary complex objects
|
||||||
into primitive representations.
|
into primitive representations.
|
||||||
"""
|
"""
|
||||||
def to_representation(self, obj):
|
def to_representation(self, instance):
|
||||||
output = {}
|
output = {}
|
||||||
for attribute_name in dir(obj):
|
for attribute_name in dir(instance):
|
||||||
attribute = getattr(obj, attribute_name)
|
attribute = getattr(instance, attribute_name)
|
||||||
if attribute_name.startswith('_'):
|
if attribute_name.startswith('_'):
|
||||||
# Ignore private attributes.
|
# Ignore private attributes.
|
||||||
pass
|
pass
|
||||||
|
@ -1010,7 +1010,7 @@ Some reasons this might be useful include...
|
||||||
|
|
||||||
The signatures for these methods are as follows:
|
The signatures for these methods are as follows:
|
||||||
|
|
||||||
#### `.to_representation(self, obj)`
|
#### `.to_representation(self, instance)`
|
||||||
|
|
||||||
Takes the object instance that requires serialization, and should return a primitive representation. Typically this means returning a structure of built-in Python datatypes. The exact types that can be handled will depend on the render classes you have configured for your API.
|
Takes the object instance that requires serialization, and should return a primitive representation. Typically this means returning a structure of built-in Python datatypes. The exact types that can be handled will depend on the render classes you have configured for your API.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user