0

I need to get post or comment count about user in specific group.

So, I use this code. It works well.

if model == 'post':
    return _group.fbuser_set.filter(posts__group=_group).annotate(count=Count('posts')).order_by('-count')
else:
    return _group.fbuser_set.filter(comments__group=_group).annotate(count=Count('comments')).order_by(
                    '-count')

And I want to get both post and comment count about user in specific group.

So, I make this code

_group.user_set.filter(posts__group=_group, comments__group=_group) \
.annotate(p_count=Count('posts'), c_count=Count('comments'))

and this code.

_group.user_set.filter(posts__group=_group) \
.annotate(p_count=Count('posts')).filter(comments__group=_group) \
.annotate(c_count=Count('comments'))

But, both of them do not work. It's out put is count in total groups.

What can I do for getting counts in specific group?

If I need to use raw sql, how can I make sql about same function?

egaoneko
  • 389
  • 1
  • 5
  • 24

1 Answers1

0

@Leistungsabfall's advice

_group.user_set.filter(posts__group=_group, comments__group=_group) \
            .annotate(p_count=Count('posts', distinct=True), c_count=Count('comments', distinct=True))

It works well!

egaoneko
  • 389
  • 1
  • 5
  • 24