Django 对除查询项之外的模型实例进行注释使用
我有一个查询,
em =Employer.objects.filter(id=1).annotate(overall_value = Sum('companyreview__overallRating'))
em[0].overall_value
如您所见,我想要对 employer
具有 id = 1 的所有
。companyreview
对象的 overallRating
字段进行求和
上面的查询满足了我的要求,但我确信有一种方法可以从 Employer
实例获取总和。
我怎样才能像这样实现这个查询
em =Employer.objects.get(id=1)
rate = em.companyreview_set.all().annotate(overall_value = Sum('overallRating'))
rate.overall_value
?
谢谢
I have a query such that
em =Employer.objects.filter(id=1).annotate(overall_value = Sum('companyreview__overallRating'))
em[0].overall_value
As you see I want to sum of overallRating
field of all companyreview
objects whose employer
has id = 1
.
The query above does what I want but I am sure that there is a way to get the sum from an Employer
instance.
How can I implement this query like
em =Employer.objects.get(id=1)
rate = em.companyreview_set.all().annotate(overall_value = Sum('overallRating'))
rate.overall_value
?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
聚合
:对于:
Use
aggregate
:For: