On Mar 16, 2011, at 8:49 AM, Margie Roginski 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. > It seems like I really need something like this (which doesn't exist): > > Queue.objects.annotate(num_tasks=Count('task__status=Task.CLOSED_STATUS')) > Would something like this work?
Queue.objects.extra( select = { 'num_tasks' = 'SELECT COUNT(*) FROM app_task WHERE app_task.queue_id = app_queue.id' and app_task.status=%s'), select_params=(Task.CLOSED_STATUS,) > > At a higher level, I am trying to find a way to sort my queues based > on the number of tasks that point to the queue of a particular status. > IE, the user would be able to sort their queues based on number of > open tasks or number of closed tasks. Perhaps there is some other > approach that I am missing ... > > Margie > > > > > On Mar 15, 2:43 am, Tom Evans <tevans...@googlemail.com> wrote: >> On Mon, Mar 14, 2011 at 8:57 PM,MargieRoginski >> >> <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 willannotateeach Queue >>> with the number of tasks whose queue field is pointing to that Queue >>> and status field has a certain value. >> >>> I don't thinkannotatewill 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 whatannotateis 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. > -- 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.