On Mon, Mar 14, 2011 at 8:57 PM, Margie Roginski
<[email protected]> 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 [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.