[Answered ]-Django count annotation field

2👍

You can’t use annotate with annotated values. However, you can use aggregate. See the docs.

Your queries will look like:

e_in_s = Subject.objects.annotate(revcount = Count('encounter')).order_by('-revcount')
sprime_in_e = e_in_s.values('revcount').aggregate(countprime=Count('revcount'))

If this doesn’t suffice you’ll need to use the extra feature or drop down to raw SQL.

Leave a comment