tags:

views:

28

answers:

1

If I have a database like one in the aggregation example in the django documentation. http://docs.djangoproject.com/en/dev/topics/db/aggregation/

How do I write a query that lists the the average number of books each publisher has published?

+1  A: 

I think this should be the way to to do it:

>>> from django.db.models import Avg
>>> Publisher.objects.annotate(num_books=Count('book')) \
...                  .aggregate(Avg('num_books'))
{'num_books__avg': 12.1}

Hope that helps!

lemonad