Skip to content

Commit b1a5294

Browse files
committed
Merge pull request #3411 from yiyocx/patch-1
Improved Serializer relations docs
2 parents 8cae462 + 7df1107 commit b1a5294

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

docs/api-guide/relations.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ For example, the following serializer:
255255
class TrackSerializer(serializers.ModelSerializer):
256256
class Meta:
257257
model = Track
258-
fields = ('order', 'title')
258+
fields = ('order', 'title', 'duration')
259259

260260
class AlbumSerializer(serializers.ModelSerializer):
261261
tracks = TrackSerializer(many=True, read_only=True)
@@ -293,7 +293,7 @@ Be default nested serializers are read-only. If you want to to support write-ope
293293
class TrackSerializer(serializers.ModelSerializer):
294294
class Meta:
295295
model = Track
296-
fields = ('order', 'title')
296+
fields = ('order', 'title', 'duration')
297297

298298
class AlbumSerializer(serializers.ModelSerializer):
299299
tracks = TrackSerializer(many=True)
@@ -405,13 +405,15 @@ In this case we'd need to override `HyperlinkedRelatedField` to get the behavior
405405
def get_url(self, obj, view_name, request, format):
406406
url_kwargs = {
407407
'organization_slug': obj.organization.slug,
408-
'customer_pk': obj.pk }
408+
'customer_pk': obj.pk
409+
}
409410
return reverse(view_name, url_kwargs, request=request, format=format)
410411

411412
def get_object(self, view_name, view_args, view_kwargs):
412413
lookup_kwargs = {
413414
'organization__slug': view_kwargs['organization_slug'],
414-
'pk': view_kwargs['customer_pk'] }
415+
'pk': view_kwargs['customer_pk']
416+
}
415417
return self.get_queryset().get(**lookup_kwargs)
416418

417419
Note that if you wanted to use this style together with the generic views then you'd also need to override `.get_object` on the view in order to get the correct lookup behavior.

0 commit comments

Comments
 (0)