Given the following models:
class Flight:
class Checklist:
flight = ForeignKey(Flight)
class Item:
checklist = ForeignKey(Checklist)
completed = BooleanField()
I need to get the number of completed checklists for each flight. A checklist is considered completed when all of its items are completed.
I know for checklists I can do
Checklist.objects.annotate(
is_complete=~Exists(Item.objects.filter(
completed=False,
checklist_id=OuterRef('pk'),
))
)
but I need something for flights, ideally a single query. Something like
Flight.objects.annotate(
completed_checklists=Count(
Checklist.objects.annotate(<is complete annotation here>).filter(is_complete=True)
)
)