开发者

Django query help needed

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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜