annotate与aggregate的区别

For example, to find the price range of books offered in each store,you could use the annotation:

以下语句查的是每一个商店中书的最高价和最低价,注意是每一个商店,结果是按商店分的数组

相当于给每条数据做注释。

>>> from django.db.models import Max, Min
>>> Store.objects.annotate(min_price=Min('books__price'), max_price=Max('books__price'))

This tells Django to retrieve the Store model, join (through themany-to-many relationship) with the Book model, and aggregate on theprice field of the book model to produce a minimum and maximum value.

The same rules apply to the aggregate() clause. If you wanted toknow the lowest and highest price of any book that is available for salein any of the stores, you could use the aggregate:

以下语句查的是所有商店中,书的最高价和最低价,注意是所有商店,或者说任意一个商店,结果是一个最大值和一个最小值。

这里是对全部数据求最大最小值,是单纯的数据聚合功能。

>>> Store.objects.aggregate(min_price=Min('books__price'), max_price=Max('books__price'))


https://docs.djangoproject.com/en/2.0/topics/db/aggregation/#joins-and-aggregates
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值