汇总column_property值

时间:2016-01-11 17:04:08

标签: sql python-2.7 sqlalchemy

我有两个column_property列,我希望在grandtotal列中汇总。我希望能够对grandtotal列进行排序和过滤。

如何汇总subtotalshipping列'值?

代码:

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

错误:

TypeError: unsupported operand type(s) for +: 'ColumnProperty' and 'ColumnProperty'

1 个答案:

答案 0 :(得分:1)

您需要汇总此列属性的expressions

grandtotal = orm.column_property(func.sum(subtotal.expression + shipping.expression))