mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-27 16:40:03 +03:00
Apply suggestions from code review
cosmetic changes Co-authored-by: Xavier Ordoquy <xordoquy@linovia.com>
This commit is contained in:
parent
d675dc5b1a
commit
5f56cc63f2
|
@ -35,12 +35,12 @@ For example, the following serializer would lead to a database hit each time eva
|
||||||
fields = ['album_name', 'artist', 'tracks']
|
fields = ['album_name', 'artist', 'tracks']
|
||||||
|
|
||||||
# For each album object, tracks should be fetched from database
|
# For each album object, tracks should be fetched from database
|
||||||
qs=Album.objects.all()
|
qs = Album.objects.all()
|
||||||
print(AlbumSerializer(qs, many=True).data)
|
print(AlbumSerializer(qs, many=True).data)
|
||||||
|
|
||||||
If `AlbumSerializer` is used to serialize a fairly large queryset with `many=True` then it could be a serious performance problem. Optimizing the queryset passed to `AlbumSerializer` with:
|
If `AlbumSerializer` is used to serialize a fairly large queryset with `many=True` then it could be a serious performance problem. Optimizing the queryset passed to `AlbumSerializer` with:
|
||||||
|
|
||||||
qs=Album.objects.prefetch_related('tracks')
|
qs = Album.objects.prefetch_related('tracks')
|
||||||
# No additional database hits required
|
# No additional database hits required
|
||||||
print(AlbumSerializer(qs, many=True).data)
|
print(AlbumSerializer(qs, many=True).data)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user