ValueError:解包的值太多odoo

问题描述

当产品名称为 ups standard 或 saver 时,我想从订单行中提取估计成本, 我总是收到此错误:ValueError:要解压缩的值太多(预期为 1) ValueError:预期的单例:sale.order.line(118,119,120,121)

我该怎么办?任何帮助请

@api.depends('product_id.name')
def _compute_estimated_cost(self):
        estim_cost = 0
        #
        for order_line in self:
            if order_line.product_id.name in ['UPS Standard','UPS Saver']:
                for word in order_line.product_id.name.split():
                    try: 
                        estim_cost += round(float(word),2)
                        self.estim_cost=estim_cost
                       
                        break
                    except:
                        continue
            else:
                pass 

解决方法

你需要改变

self.estim_cost=estim_cost

order_line.estim_cost=estim_cost
,

你在这一行错了:

self.estim_cost=estim_cost

您应该使用 order_line 而不是 self。 这里 self 是 multiton sale.order.line(118,119,120,121) 在这种情况下你不能使用做作运算符。
PS:如果你想在记录集中写一个值,使用方法.write()