SELECT *,
(SELECT sum(amount) FROM history
WHERE history_id IN
(SELECT history_id FROM web_cargroup
WHERE group_id = a.group_id) AND type = 1)
as sum
FROM web_car a;
将上述查询转换为orm非常困难 1. orm annotate由group by自动创建。 2.将子查询置于“处于”状态
是很困难的请帮忙。
答案 0 :(得分:0)
如果我理解您提供的模型,这应该可行
from django.db.models import Sum
History.objects.filter(
type=1,
id__in=(CarGroup.objects.values('history_id'))
).aggregate(
total_amount=Sum('amount')
)