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

Odoo 17 CRM看板卡片渲染One2many字段报错求助

Odoo 17 解决CRM看板视图循环One2many字段的OwlError问题

问题根源

出现Invalid loop expression: 'undefined' is not iterable错误,核心原因是:

  • 视图未加载quote_ids字段,导致模板中record.quote_ids为undefined
  • 未处理关联数据为空的场景,直接循环未初始化的变量

正确实现步骤

1. 确保模型字段定义正确

首先确认crm.lead的One2many字段和关联模型insurance.quotes的Many2one字段配置无误:

CRM Lead 模型继承代码

from odoo import models, fields

class CRMLead(models.Model):
    _inherit = 'crm.lead'

    quote_ids = fields.One2many(
        'insurance.quotes', 
        'lead_id', 
        string='Insurance Quotes'
    )

Insurance Quotes 模型代码

from odoo import models, fields

class InsuranceQuotes(models.Model):
    _name = 'insurance.quotes'
    _description = 'Insurance Quotes'

    lead_id = fields.Many2one(
        'crm.lead', 
        string='CRM Lead', 
        required=True, 
        ondelete='cascade'
    )
    name = fields.Char(string='Quote Name', required=True)
    # 其他业务字段...

2. 继承CRM看板视图并配置字段加载

必须在看板视图中明确加载quote_ids字段,否则Odoo不会自动获取该关联数据:

<odoo>
    <record id="crm_lead_kanban_inherit" model="ir.ui.view">
        <field name="name">crm.lead.kanban.inherit</field>
        <field name="model">crm.lead</field>
        <field name="inherit_id" ref="crm.crm_lead_kanban_view"/>
        <field name="arch" type="xml">
            <!-- 给看板视图添加quote_ids字段到加载列表 -->
            <xpath expr="//kanban" position="attributes">
                <attribute name="fields" position="append">,quote_ids</attribute>
            </xpath>

            <!-- 在看板卡片中插入报价列表 -->
            <xpath expr="//div[hasclass('o_kanban_record_details')]" position="inside">
                <div class="mt-3">
                    <h6 class="text-muted">Insurance Quotes</h6>
                    <!-- 先判断数据是否存在,避免undefined报错 -->
                    <t t-if="record.quote_ids.raw_value">
                        <ul class="list-unstyled mt-1">
                            <t t-foreach="record.quote_ids.raw_value" t-as="quote">
                                <li class="small">
                                    <span t-esc="quote.name"/>
                                </li>
                            </t>
                        </ul>
                    </t>
                    <t t-else="">
                        <p class="small text-muted">No quotes available</p>
                    </t>
                </div>
            </xpath>
        </field>
    </record>
</odoo>

关键细节说明

  • 字段加载:通过position="append"给原看板视图的fields属性添加quote_ids,避免覆盖原有字段配置
  • 数据判断:使用t-if="record.quote_ids.raw_value"确保只有当关联数据存在时才执行循环,防止undefined场景
  • 模板语法:Odoo 17看板视图基于Owl,One2many字段的原始数据通过raw_value访问,直接循环该数组即可渲染关联记录

内容的提问来源于stack exchange,提问作者hardik device

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 05:22:06