【问题标题】:ValueError: too many values to unpack (odoo)ValueError:解包的值太多(odoo)
【发布时间】:2021-04-23 10:53:57
【问题描述】:

当产品名称为 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 

【问题讨论】:

  • 能否请您包含完整的堆栈跟踪(并将其格式化为代码)?

标签: python odoo


【解决方案1】:

你需要改变

self.estim_cost=estim_cost

order_line.estim_cost=estim_cost

【讨论】:

  • @md1999 为什么要将产品名称(字符串类型)转换为浮点数?在您的情况下,您的内部 for 循环 中的 word 只会是 UPS、Standard 或 Saver
  • 嗨,因为我想提取模型销售订单行中的 ups standard 或 saver 产生的估计成本
【解决方案2】:

这行你错了:

self.estim_cost=estim_cost

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

【讨论】:

  • 它不起作用,直到得到错误太多预期的值和 ValueError: Expected singleton: sale.order.line(132, 133, 134)...
猜你喜欢
  • 1970-01-01
  • 2016-05-11
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
相关资源
最近更新 更多