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]) >
db.execute(\"SELECT SUM(price * qty) FROM inventory_orderline WHERE order_id = %s\", [self.id])
It's not clearly documented, but this can be done with the F() function:
F()
from django.db.models import F self.line_items.annotate(lineprice=F('orderline__price') * F('orderline__qty')).aggregate(Sum('lineprice'))