Django annotation with nested filter

前端 未结 3 1048
萌比男神i
萌比男神i 2021-01-01 12:57

Is it possible to filter within an annotation?

In my mind something like this (which doesn\'t actually work)

Student.objects.all().annotate(Count(\'at

3条回答
  •  暖寄归人
    2021-01-01 13:13

    Maybe this will work for you:

    excused = Student.objects.filter(attendance__type='Excused').annotate(abs=Count('attendance'))
    

    You need to filter the Students you're looking for first to just those with excused absences and then annotate the count of them.

    Here's a link to the Django Aggregation Docs where it discusses filtering order.

提交回复
热议问题