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 开发者_JAVA技巧a query that lists the the average number of books each publisher has published?
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!
精彩评论