Django: problem with merging querysets after annotation
Posted
by Björn Lilja
on Stack Overflow
See other posts from Stack Overflow
or by Björn Lilja
Published on 2010-03-09T22:06:18Z
Indexed on
2010/05/23
12:00 UTC
Read the original article
Hit count: 284
Hi
I have a manager for "Dialog" looking like this:
class AnnotationManager(models.Manager):
def get_query_set(self):
return super(AnnotationManager, self).get_query_set().annotate(
num_votes=Count('vote', distinct=True),
num_comments=Count('comment', distinct=True),
num_commentators = Count('comment__user', distinct=True),
)
Votes and Comments has a ForeignKey to Dialog. Comments has a ForeignKey to User. When I do this:
dialogs_queryset = Dialog.public.filter(organization=organization)
dialogs_popularity = dialogs_queryset.exclude(num_comments=0) | dialogs_queryset.exclude(num_votes=0)
...dialogs_popularity will never returned the combination, but only the dialogs with more than 0 comments, or if I change the order of the OR, the dialogs with more than 0 votes!
To me, the expected behavior would be to get the dialogs with more than 0 votes AND the dialogs with more than 0 comments.
What am I missing? Or is there a bug in the annotation behavior here?
© Stack Overflow or respective owner