aboutsummaryrefslogtreecommitdiffstats
path: root/docs/api-guide/relations.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/api-guide/relations.md')
-rw-r--r--docs/api-guide/relations.md24
1 files changed, 12 insertions, 12 deletions
diff --git a/docs/api-guide/relations.md b/docs/api-guide/relations.md
index 25fca475..623fe1a9 100644
--- a/docs/api-guide/relations.md
+++ b/docs/api-guide/relations.md
@@ -43,7 +43,7 @@ In order to explain the various types of relational fields, we'll use a couple o
For example, the following serializer.
- class AlbumSerializer(serializer.ModelSerializer):
+ class AlbumSerializer(serializers.ModelSerializer):
tracks = RelatedField(many=True)
class Meta:
@@ -75,7 +75,7 @@ This field is read only.
For example, the following serializer:
- class AlbumSerializer(serializer.ModelSerializer):
+ class AlbumSerializer(serializers.ModelSerializer):
tracks = PrimaryKeyRelatedField(many=True, read_only=True)
class Meta:
@@ -109,7 +109,7 @@ By default this field is read-write, although you can change this behavior using
For example, the following serializer:
- class AlbumSerializer(serializer.ModelSerializer):
+ class AlbumSerializer(serializers.ModelSerializer):
tracks = HyperlinkedRelatedField(many=True, read_only=True,
view_name='track-detail')
@@ -149,7 +149,7 @@ By default this field is read-write, although you can change this behavior using
For example, the following serializer:
- class AlbumSerializer(serializer.ModelSerializer):
+ class AlbumSerializer(serializers.ModelSerializer):
tracks = SlugRelatedField(many=True, read_only=True, slug_field='title')
class Meta:
@@ -223,12 +223,12 @@ Note that nested relationships are currently read-only. For read-write relation
For example, the following serializer:
- class TrackSerializer(serializer.ModelSerializer):
+ class TrackSerializer(serializers.ModelSerializer):
class Meta:
model = Track
fields = ('order', 'title')
- class AlbumSerializer(serializer.ModelSerializer):
+ class AlbumSerializer(serializers.ModelSerializer):
tracks = TrackSerializer(many=True)
class Meta:
@@ -265,7 +265,7 @@ For, example, we could define a relational field, to serialize a track to a cust
duration = time.strftime('%M:%S', time.gmtime(value.duration))
return 'Track %d: %s (%s)' % (value.order, value.name, duration)
- class AlbumSerializer(serializer.ModelSerializer):
+ class AlbumSerializer(serializers.ModelSerializer):
tracks = TrackListingField(many=True)
class Meta:
@@ -295,14 +295,14 @@ Note that reverse relationships are not automatically generated by the `ModelSer
**The following will not work:**
- class AlbumSerializer(serializer.ModelSerializer):
+ class AlbumSerializer(serializers.ModelSerializer):
class Meta:
fields = ('tracks', ...)
Instead, you must explicitly add it to the serializer. For example:
- class AlbumSerializer(serializer.ModelSerializer):
- tracks = serializers.PrimaryKeyRelationship(many=True)
+ class AlbumSerializer(serializers.ModelSerializer):
+ tracks = serializers.PrimaryKeyRelatedField(many=True)
...
By default, the field will uses the same accessor as it's field name to retrieve the relationship, so in this example, `Album` instances would need to have the `tracks` attribute for this relationship to work.
@@ -315,8 +315,8 @@ The best way to ensure this is typically to make sure that the relationship on t
Alternatively, you can use the `source` argument on the serializer field, to use a different accessor attribute than the field name. For example.
- class AlbumSerializer(serializer.ModelSerializer):
- tracks = serializers.PrimaryKeyRelationship(many=True, source='track_set')
+ class AlbumSerializer(serializers.ModelSerializer):
+ tracks = serializers.PrimaryKeyRelatedField(many=True, source='track_set')
See the Django documentation on [reverse relationships][reverse-relationships] for more details.