两个字段注释的产品 [英] Product of two fields annotation

查看:93
本文介绍了两个字段注释的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Django应用程序中有一行:

I currently have a line in my Django app like this:

db.execute("SELECT SUM(price * qty) FROM inventory_orderline WHERE order_id = %s", [self.id])

我宁愿执行这通过模型界面提供了我的Django,但是找不到任何引用。

I'd rather perform this through the models interface provided my Django, but can't find any references.

我很确定这可以用注释完成,但是例子只覆盖其中一些,我在文档中找不到列表。

I'm pretty sure this can be done with an annotation, but the examples only cover a few of them and I can't find a list in the documentation.

我想做这样的事情:

self.line_items.annotate(lineprice=Product('orderline__price', 'orderline__qty')).aggregate(Sum('lineprice'))

任何人都可以建议使用注释类来执行乘法?更好的是,链接到API列出所有这些注释/聚合类?

Can anyone suggest an annotation class to use to perform the multiplication? Even better, a link to the API listing all these annotation/aggregation classes?

推荐答案

不幸的是,是不够的内置的聚合功能,具体来说,没有一个产品

Unfortunately, there aren't enough inbuilt Aggregate functions and specifically, there is not one for Product.

但是,除了必须编写不简明的ORM查询之外,这并不限制您。特别针对您的情况,您应该可以做到:

But that doesn't limit you in any way other than having to write a "non-concise" ORM query. Specifically for your case, you should be able to do:

self.line_items.extra(select=("lineprice": "orderline__price*orderline__qty")).aggregate(Sum('lineprice'))

这篇关于两个字段注释的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆