Product of two fields annotation

后端 未结 2 1164
情话喂你
情话喂你 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:36

    It's not clearly documented, but this can be done with the F() function:

    from django.db.models import F
    
    self.line_items.annotate(lineprice=F('orderline__price') * F('orderline__qty')).aggregate(Sum('lineprice'))
    

提交回复
热议问题