Is there a way for field models related to preliminary search?

I am showing the API for a specific model and want to serialize some fields related to it. These related fields are usually repeated, and I do not want to perform multiple db queries for each related field serialization. Is there an easy way to pre-query all related instances and then you have a RelatedField serializer in it? Or maybe point out a related field from ModelSerializer?

+7
source share
1 answer

You can use the standard prefetch_related and select_related parameters in your request.

In the view, use the queryset attribute, not the model shortcut. For example...

 class ExampleView(generics.ListCreateAPIView): serializer_class = ExampleSerializer queryset = Example.objects.select_related(...) 
+5
source

All Articles