Django 1.3, Python 2.7
I have the following models (some irrelevant fields omitted):
class Encounter(models.Model):
subject = models.ForeignKey('Subject', to_field='uuid')
uuid = models.SlugField(max_length=36, unique=True, default=make_uuid, editable=False)
class Subject(models.Model):
uuid = models.SlugField(max_length=36, unique=True, default=make_uuid, editable=False)
I calculated the number of encounters per subject using the following code:
e_in_s = Subject.objects.annotate(revcount = Count('encounter')).order_by('-revcount')
Now I would like to know how many subjects have exactly revcount
encounters for each value of revcount
. I tried the following:
sprime_in_e = e_in_s.values('revcount').annotate(countprime=Count('revcount'))
but got the error "Cannot compute Count('revcount'): 'revcount' is an aggregate". Any suggestions?
Note: I need the value produced by e_in_s
as well as its annotation.