I got a simple requirement (not a simple implementation) and figured out how to achieve it without making multiple db calls and without .extra() in the query set.
Task: name = xxx status = models.IntegerField(choices=some_choices) project = ForeignKey(Project) Project: name = xxx code = xxx
Projects contain tasks that have different statuses. (Assume status = 3 completed) Now I want to list all the projects with their full tasks and completed tasks, for example below
- Project 1, total_tasks = 5, completed_tasks = 2
- Project 1, total_tasks = 2, completed_tasks = 1
I can get total_tasks with annotation but not completed tasks since the condition is required in the annotation. Is there any way to do this?
source share