对列属性值求和

3

我有两个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个回答

2
您需要对该列属性的表达式求和。
grandtotal = orm.column_property(func.sum(subtotal.expression + shipping.expression))

谢谢老兄!我还需要按订单ID对grandtotal列进行分组。grandtotal = orm.column_property(select([func.sum(subtotal.expression + shipping.expression)]).group_by(id)) - Colton Allen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接