如何在当前未保存的订单行上使用odoo onchange方法?

问题描述

我正在使用折扣值更新销售订单行,并且有些产品没有折扣选项,这意味着在分发折扣值时忽略该行

这是方法

@api.onchange('discount_type','discount_rate','order_line','discount_durar')
def set_lines_discount(self):
    total = discount = 0.0
    for line in self.order_line:
        if self.discount_type == 'percentage':
            if line.no_discount_field:
                line.discount_durar = 0
            else:
                line.discount_durar = self.discount_rate
        else:
            if line.no_discount_field == True:
                line.discount_durar = 0
            else:
                total += (line.product_uom_qty * line.price_unit)
                if self.discount_rate != 0:
                    discount = (self.discount_rate / total) * 100
                else:
                    discount = self.discount_rate
                for line in self.order_line.search([('no_discount_field','=',False)]):
                    line.discount_durar = discount

它在所有条件下都能正常工作,除了我使用的最后一个条件,它需要保存顺序才能计算出来并遍历所有行。

在保存订单之前,如何在条件中循环?

解决方法

您可以使用带条件的python单行列表理解。在您的情况下,您可以拥有类似的东西。

[line for line in self.order_line if condition]
,

我使用过滤器更改了sale.order.line搜索对象

for line in self.order_line.filtered(lambda l: not l.no_discount_field):

然后效果很好

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...