I have this `view` ``` def get_queryset(self) -> QuerySet[Good]: .... qs = ( Good.objects.values('brand_id', 'brand__name') .annotate( .... ) .prefetch_related(Prefetch('history', StocksHistory.objects.filter(Q(**subquery_filter_args)))) .order_by('-total_sales') ) return qs
``` and serializer ``` class ExtendedBrandSerializer(serializers.ModelSerializer): ... history = serializers.SerializerMethodField() class Meta: model = Good fields = ( ... 'history', ) def get_history(self, good: dict) -> dict: .... return StocksHistorySerializer( StocksHistory.objects.extra(select={'day': 'date( snap_at )'}) .values('day') .filter(history_filter_query) .annotate( .... ), many=True, ).data ``` **Relation**: `StocksHistory (*) -> (1) Good`. I have `N+1` queries in `SerializerMethodField`. How can I fix it? Perhaps there is a way to move annotate from serializer to view? The bottom line is that I also need the history key in the response, which will contain a list of these child objects. Queries - https://gist.github.com/dima-dmytruk23/0183014d76ad25f4e2a0ca87ec225a10 Also can I use RAW SQL for it maybe? -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/edc0d307-ebd5-4d14-a32c-e87f6ae81a5dn%40googlegroups.com.