Django Count() in multiple annotations
I think Count('topics', distinct=True)
should do the right thing. That will use COUNT(DISTINCT topic.id)
instead of COUNT(topic.id)
to avoid duplicates.
User.objects.filter(
username_startswith="ab").annotate(
posts=Count('post', distinct=True)).annotate(
topics=Count('topic', distinct=True)).values_list(
"username","posts", "topics")