I have a model that represents interdependent tasks. A task cannot be started until all of its prerequisite tasks have been completed. I want to find all the tasks which are ready to start, i.e. all their prerequisite tasks (if any) have been completed. This is the relevant part of the model:
class Task(models.model): TASK_STATUS_CHOICES = ( (u"P", u'Pending'), (u"A", u'Assigned'), (u"C", u'Complete'), (u"F", u'Failed') ) status = models.CharField(max_length=1, choices=TASK_STATUS_CHOICES) prerequisites = models.ManyToManyField('self', symmetrical=False, related_name="dependants") I want to write a query that will return the set of all tasks for which *all* prerequisite tasks are complete. So far I can only figure out how to get the set of all tasks for which *any* prerequisite task is complete: Task.objects.filter(prerequisites__status=u"C") How do I find the set of tasks for which all prerequisite tasks are complete? Do I need to resort to writing SQL? Is there some way to apply a filter before an annotation? For example, here I can annotate each task with its number of prerequisites: Task.objects.annotate(prereq_count=Count('prerequisites')) Is there a way to make this aggregation count only the prerequisite tasks that are not complete, i.e. to annotate each task with a count of the number of prerequisite tasks it has for which status is not equal to "C"? (Note: I have also asked this question on StackOverflow. Here is the link: http://stackoverflow.com/questions/1516795/in-django-how-do-i-filter-based-on-all-entities-in-a-manytomany-relation-instead ) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---