I have a model like this:
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=2, choices=TASK_STATUS_CHOICES) prerequisites = models.ManyToManyField('self', symmetrical=False, related_name="dependents")
I want to find all the tasks, all the prerequisites of which are completed. I tried:
Task.objects.filter(prerequisites__status=u"C")
This gets all the tasks for which any precondition is fulfilled. I thought maybe I need to use the annotation, but I donβt see how to apply the filter to the necessary requirements before performing the aggregation. For example, I can find the number of prerequisites for each task, for example:
Task.objects.annotate(prereq_count=Count('prerequisites'))
But how can I comment on tasks with the number of prerequisites that have a status not equal to "C"?
source share