Right again!  I agree what you are saying works.  I tried to take it a
step further and OR together two querysets.  I was then looking at the
results of that OR, and it was not what I expected.  One qs had been
annotated with num_tasks_open, the other had been annotated with
num_tasks_closed.  I was thinking I could just OR them together and
get a single queryset where each queue would would be annotated with
num_tasks_open and num_tasks_closed.  But that does not seem to work.

> qsOpen = Queue.objects.filter(name="foo", 
> task__status=Task.OPEN_STATUS).annotate(num_tasks_open=Count('task'))
> qsClosed = Queue.objects.filter(name="foo", 
> task__status=Task.CLOSED_STATUS).annotate(num_tasks_closed=Count('task'))
> qsOpen[0].num_tasks_open
Out[37]: 1

> qsClosed[0].num_tasks_closed
Out[38]: 4

> newQs = qsOpen | qsClosed

> newQs[0].num_tasks_open <<== Was expecting this to still show 1 task open
Out[41]: 5

> newQs[0].num_tasks_closed  <<== Was expecting this to show 4 tasks open
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call
last)
/home/mlevine/django/chipvision74/chip_vision_2/<ipython console> in
<module>()
AttributeError: 'Queue' object has no attribute 'num_tasks_closed'


Margie


On Mar 16, 9:13 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Wed, Mar 16, 2011 at 3:49 PM, Margie Roginski
>
> <margierogin...@yahoo.com> wrote:
> > Hmmm ... so I just got back to trying this and I actually don't think
> > the annotate call as suggested does what I am looking for.  Say I want
> > to annotate queues with the number of closed tasks. This call:
>
> > Queue.objects.filter(task__status=Task.STATUS_CLOSED).annotate(num_tasks=Count('task'))
>
> > finds all queues that have tasks that are closed, but then it
> > annotates the queue with the total number of tasks that point to the
> > queue, not just the number of closed tasks that point to the queue.
>
> No, you are incorrect. An example with similar models:
>
> >>> qs = TVSeries.objects.filter(name='South 
> >>> Park').filter(tvepisode__title__contains='hero').annotate(num_episodes_with_hero_in_title=Count('tvepisode'))
> >>> qs[0].num_episodes_with_hero_in_title
> 1
> >>> qs2 = TVSeries.objects.filter(name='South 
> >>> Park').annotate(num_episodes=Count('tvepisode'))
> >>> qs2[0].num_episodes
>
> 202
>
> As you can see, the annotate is clearly correctly affected by the
> earlier filter.
>
> Cheers
>
> Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to