开发者

Django query help needed

开发者 https://www.devze.com 2022-12-21 21:31 出处:网络
If I have a database like one in the aggregation example in the django documentation. http://docs.djangoproject.com/en/dev/topics/db/aggregation/

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

精彩评论

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