I have two column_property columns that I'd like to sum together in the grandtotal column. I want to be able to sort and filter against the grandtotal column.
How can I sum the subtotal and shipping columns' values?
Code:
subtotal = orm.column_property(
select([case(
[(func.sum(OrderProductModel.subtotal).is_(None), 0)],
else_=func.sum(OrderProductModel.subtotal))
]).where(OrderProductModel.order_id == id))
shipping = orm.column_property(case(
[(is_expedited.is_(True), shipping_rate)], else_=Decimal(0.00)))
grandtotal = orm.column_property(func.sum(subtotal + shipping))
Error:
TypeError: unsupported operand type(s) for +: 'ColumnProperty' and 'ColumnProperty'
You need to summing expressions of this column properties
grandtotal = orm.column_property(func.sum(subtotal.expression + shipping.expression))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With