Ah, right - so obvious!  I'm not sure why it didn't occur to me to
filter first, then annotate.  Thank you!


On Mar 15, 2:43 am, Tom Evans <tevans...@googlemail.com> wrote:
> On Mon, Mar 14, 2011 at 8:57 PM, Margie Roginski
>
> <margierogin...@yahoo.com> wrote:
> > class Queue(models.Model):
> >  # fields here, not relevant for this discussion
>
> > class Task(models.Mode):
> >  queue = models.ForeignKey("Queue")
> >  status = models.IntegerField(choices=STATUS_CHOICES)
>
> > I am trying to create a Queue queryset that will annotate each Queue
> > with the number of tasks whose queue field is pointing to that Queue
> > and status field has a certain value.
>
> > I don't think annotate will work for me due to me needing to count up
> > only tasks whose status field has a certain value.  I think I might
> > need extra?  But I'm having touble making that work.
>
> No, this is precisely what annotate is for.
>
> Queue.objects.filter(task__status=Task.SOME_CHOICE).annotate(num_tasks=Count('task'))
>
> 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