When I try to annotate my model I face an issue that two fields multiply each other
def get_queryset(self):
return self.queryset.annotate(
my_votes=Count("votes", filter=Q(votes=self.request.user), distinct=False)
vote_count=Count("votes", distinct=False)
comments_count=Count("comments", distinct=True)
)
I know that there is an issue with multiple aggregations
Combining multiple aggregations with annotate() will yield the wrong results because joins are used instead of subqueries
For example, if there are 3 comments on the post and I voted on the post 4 times and there are 7 votes total the count will return:
my_votes=12 (4 my_votes * 3 comments)
vote_count=21 (7 votes * 3 comments)
comments_count=3
if I remove the comments count everything works as it should.
if there are 0 comments it also counts properly
There are ManyToMany relations between Post and Vote
The is ForignKey between Comment and Post
Is there another way to accomplish this?
Thank you for your help.