Product of two fields annotation

后端 未结 2 1161
情话喂你
情话喂你 2021-01-18 17:00

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])
         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 17:14

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

    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'))
    

提交回复
热议问题