[Answered ]-Django Count on FK with Sum and filters

1👍

Sum is a aggregate function which means keeps in process all values for a column in match with query.

But you would like to sum current row’s 2 column.

So use

ts = F('examination_participations__total_score') + F('examination_participations__additional_score')

instead of

ts = Sum(F('examination_participations__total_score') + F('examination_participations__additional_score'))

0👍

I found following solution:

def count_sufficient_participations(self):
    qs = self.annotate(
        cnt_suff=Count('examination_participations', filter=Q(total_score__lte=(F('examination_participations__total_score') + F('examination_participations__additional_score')) * 2))
    )
    return qs

Thanks for your help.

Leave a comment