mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-22 09:36:49 +03:00
parent
355afcf64b
commit
374c0d4142
|
@ -56,7 +56,7 @@ In order to explain the various types of relational fields, we'll use a couple o
|
||||||
|
|
||||||
`StringRelatedField` may be used to represent the target of the relationship using its `__str__` method.
|
`StringRelatedField` may be used to represent the target of the relationship using its `__str__` method.
|
||||||
|
|
||||||
For example, the following serializer.
|
For example, the following serializer:
|
||||||
|
|
||||||
class AlbumSerializer(serializers.ModelSerializer):
|
class AlbumSerializer(serializers.ModelSerializer):
|
||||||
tracks = serializers.StringRelatedField(many=True)
|
tracks = serializers.StringRelatedField(many=True)
|
||||||
|
@ -65,7 +65,7 @@ For example, the following serializer.
|
||||||
model = Album
|
model = Album
|
||||||
fields = ['album_name', 'artist', 'tracks']
|
fields = ['album_name', 'artist', 'tracks']
|
||||||
|
|
||||||
Would serialize to the following representation.
|
Would serialize to the following representation:
|
||||||
|
|
||||||
{
|
{
|
||||||
'album_name': 'Things We Lost In The Fire',
|
'album_name': 'Things We Lost In The Fire',
|
||||||
|
@ -291,7 +291,7 @@ Would serialize to a nested representation like this:
|
||||||
|
|
||||||
## Writable nested serializers
|
## Writable nested serializers
|
||||||
|
|
||||||
By default nested serializers are read-only. If you want to support write-operations to a nested serializer field you'll need to create `create()` and/or `update()` methods in order to explicitly specify how the child relationships should be saved.
|
By default nested serializers are read-only. If you want to support write-operations to a nested serializer field you'll need to create `create()` and/or `update()` methods in order to explicitly specify how the child relationships should be saved:
|
||||||
|
|
||||||
class TrackSerializer(serializers.ModelSerializer):
|
class TrackSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
|
@ -343,7 +343,7 @@ To provide a dynamic queryset based on the `context`, you can also override `.ge
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
For example, we could define a relational field to serialize a track to a custom string representation, using its ordering, title, and duration.
|
For example, we could define a relational field to serialize a track to a custom string representation, using its ordering, title, and duration:
|
||||||
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
@ -359,7 +359,7 @@ For example, we could define a relational field to serialize a track to a custom
|
||||||
model = Album
|
model = Album
|
||||||
fields = ['album_name', 'artist', 'tracks']
|
fields = ['album_name', 'artist', 'tracks']
|
||||||
|
|
||||||
This custom field would then serialize to the following representation.
|
This custom field would then serialize to the following representation:
|
||||||
|
|
||||||
{
|
{
|
||||||
'album_name': 'Sometimes I Wish We Were an Eagle',
|
'album_name': 'Sometimes I Wish We Were an Eagle',
|
||||||
|
@ -535,7 +535,7 @@ And the following two models, which may have associated tags:
|
||||||
text = models.CharField(max_length=1000)
|
text = models.CharField(max_length=1000)
|
||||||
tags = GenericRelation(TaggedItem)
|
tags = GenericRelation(TaggedItem)
|
||||||
|
|
||||||
We could define a custom field that could be used to serialize tagged instances, using the type of each instance to determine how it should be serialized.
|
We could define a custom field that could be used to serialize tagged instances, using the type of each instance to determine how it should be serialized:
|
||||||
|
|
||||||
class TaggedObjectRelatedField(serializers.RelatedField):
|
class TaggedObjectRelatedField(serializers.RelatedField):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user