Optimizing the queryset passed to AlbumSerializer with: qs = _related('tracks') If AlbumSerializer is used to serialize a fairly large queryset with many=True then it could be a serious performance problem. Print(AlbumSerializer(qs, many=True).data) # For each album object, tracks should be fetched from database It is the programmer's responsibility to optimize queries to avoid additional database hits which could occur while using such a serializer.įor example, the following serializer would lead to a database hit each time evaluating the tracks field if it is not prefetched: class AlbumSerializer(serializers.ModelSerializer):įields = A serializer with a field spanning an orm relation through its source attribute could require an additional database hit to fetch related objects from the database. Note: REST Framework does not attempt to automatically optimize querysets passed to serializers in terms of select_related and prefetch_related since it would be too much magic. Note: The relational fields are declared in relations.py, but by convention you should import them from the serializers module, using from rest_framework import serializers and refer to fields as serializers. They can be applied to ForeignKey, ManyToManyField and OneToOneField relationships, as well as to reverse relationships, and custom relationships such as GenericForeignKey. Relational fields are used to represent model relationships. Data structures, not algorithms, are central to programming.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |