I got simple requirement (not simple implementation), and figuring out how to achieve it without making multiple hits to db, and without .extra()
in queryset.
Task:
name = xxx
status = models.IntegerField(choices=some_choices)
project = ForeignKey(Project)
Project:
name = xxx
code = xxx
Projects contain Tasks which got various statuses. (Assume status=3 is Completed) Now, I want to list out all projects with their total tasks and completed tasks, like below
- Project 1, total_tasks=5, completed_tasks=2
- Project 1, total_tasks=2, completed_tasks=1
I am able to get total_tasks with annotate, but not completed_tasks, since it required condition in annotation. Is there anyway to do it?