You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo代码报错ValueError: Expected singleton,请求排查原因

Odoo中ValueError: Expected singleton错误排查与解决

错误原因

  • select_hargamitra是多记录集:search()返回多条isd.res.partner.price_list记录时,直接访问select_hargamitra.price_list会触发singleton错误——Odoo不允许对多记录集直接访问普通字段(仅计算字段返回集合时例外)。
  • 循环逻辑混乱:代码直接遍历select_hargamitra.price_list,但外层的select_hargamitra可能包含多条记录,访问字段时直接触发异常。

修复后的代码

@api.model
def update_prices_in_mitra(self):
    self.ensure_one()
    # 筛选当前mitra对应的所有价格列表记录
    price_list_records = self.env['isd.res.partner.price_list'].search([
        ('partner_id', '=', self.mitra_id.id)
    ])
    # 遍历每条价格列表主记录
    for price_record in price_list_records:
        # 遍历当前主记录下的价格明细项
        for price_item in price_record.price_list:
            # 匹配当前记录的产品
            if price_item.product_id.id == self.product_id.id:
                # 更新对应的订单行
                for line in self.task_orderlines:
                    line.write({
                        'mitra_price': price_item.price_amount,
                        'isd_mitra_status': True
                    })

关键修改说明

  1. 遍历单记录字段:先遍历price_list_records中的每条记录,再访问单记录的price_list字段,彻底避免多记录集访问字段的singleton错误。
  2. 简化逻辑:移除冗余的select_hargamitra and len(...)判断——search()返回空记录集时循环不会执行,无需额外判断。
  3. 逻辑清晰化:调整循环顺序,先遍历价格列表主记录,再遍历明细项,符合数据关联关系,避免逻辑混淆。

额外排查点

  • 确认self.product_id是单记录(many2one字段),如果是其他字段类型需保证访问id时是单值。
  • 检查isd.res.partner.price_list模型的price_list字段是否为one2many类型,确保字段访问方式正确。

内容的提问来源于stack exchange,提问作者Ahmad Daud Juned

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 00:50:20