【问题标题】:how use odoo onchange method on current non saved order lines?如何在当前未保存的订单行上使用 odoo onchange 方法?
【发布时间】:2020-12-16 18:18:48
【问题描述】:

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

方法如下

@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 odoo onchange odoo-12


    【解决方案1】:

    您可以使用带有条件的 python 单行列表推导。在你的情况下,你可以有这样的东西。

    [line for line in self.order_line if condition]
    

    【讨论】:

    • 你能定义线的条件吗?
    • line.no_discount_field 为假
    • 以下代码行将为您提供 no_discount_field 为 False 的行列表 no_discount_field_false_list = [line for line in self.order_line if line for line in self.order_line == False] 然后您可以检查如果列表不为空,请执行分配 line.discount_durar = discount if no_discount_field_false_list: line.discount_durar = discount
    【解决方案2】:

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

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

    然后它运行良好

    【讨论】:

      猜你喜欢
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 2020-12-13
      相关资源
      最近更新 更多