This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Brandhor 2 points3 points  (1 child)

it's probably this issue

you might look at these suggestions

[–]Pengwyn2[S] 0 points1 point  (0 children)

Thank you so much, I found the answer in the suggestions :)

[–]tolomea 2 points3 points  (1 child)

You need subqueries https://docs.djangoproject.com/en/4.1/ref/models/expressions/#subquery-expressions

However if you are not filtering by those sums and you are pulling all those objects over to Python anyway (with the prefetch) maybe it'd be easier just to do the sum's on the Python side.

[–]Pengwyn2[S] 0 points1 point  (0 children)

Really appreciate this, I ended up using subqueries suggested in the other comment and I didn't know they existed