[Solved]-Django subquery throws "more than one row returned by a subquery used as an expression"

14đź‘Ť

Apparently, I perceived the meaning of .values('project') differently/wrongly.

So the following worked for me.

private_donations = PrivateDonation.objects.filter(project=OuterRef('pk')).order_by().values('project')
private_donations_sum = private_donations.annotate(pd_sum=Sum('value')).values('value')
projects = Project.objects.annotate(sum=Subquery(private_donations_sum))
👤sajid

Leave a comment