Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Summing column_property values

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'
like image 671
Colton Allen Avatar asked Feb 13 '26 22:02

Colton Allen


1 Answers

You need to summing expressions of this column properties

grandtotal = orm.column_property(func.sum(subtotal.expression + shipping.expression))
like image 108
r-m-n Avatar answered Feb 15 '26 11:02

r-m-n



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!