我在 crm.lead 模型中添加了一个自定义 one2many 字段:
class CrmLead(models.Model):
_inherit = 'crm.lead'
quote_ids = fields.One2many('insurance.quotes', 'crm_lead_id', string='Quotes')
相关型号代码为:
class InsuranceQuotes(models.Model):
_name = 'insurance.quotes'
_description = 'Insurance Quotes'
crm_lead_id = fields.Many2one('crm.lead', string='CRM Lead')
broker_id = fields.Many2one('res.partner', string='Broker', domain="[('is_broker', '=', True)]", context={'default_is_broker': True})
我的看板视频代码是:
<record id="view_crm_lead_kanban_inherit_custom" model="ir.ui.view">
<field name="name">crm.lead.kanban.inherit.custom</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_kanban_view_leads"/>
<field name="arch" type="xml">
<xpath expr="//div[hasclass('o_kanban_record_bottom')]" position="after">
<div class="oe_kanban_bottom_right o_kanban_bottom_extra">
<t t-set="record" t-value="record"/>
<li t-foreach="record.quote_ids" t-as="quote">
<p t-esc="quote.partner_id.name"/>
</li>
</div>
</xpath>
</field>
</record>
打开 crm 模块时出现以下错误:
UncaughtPromiseError > OwlError
Uncaught Promise > Invalid loop expression: "undefined" is not iterable
任何人都可以帮助我正确的 odoo 17 版本语法吗? TIA
您可以尝试用字段组件替换 foreach 循环,并在新的计算字段中显示您想要的 html 结果...
在您的模型中添加新的计算字段
class CrmLead(models.Model):
_inherit = 'crm.lead'
quote_ids = fields.One2many('jca.owl.cart.insurance.quotes', 'crm_lead_id', string='Quotes')
quote_ids_ul = fields.Html(string='Quotes li', compute='_compute_quote_li')
@api.depends('quote_ids')
def _compute_quote_li(self):
ul=""
for lead in self:
for quote in lead.quote_ids:
ul+= f"<li>{quote.broker_id.name}</li>"
lead.quote_ids_ul=ul
然后在您的 xml 视图中...
<record id="view_crm_lead_kanban_inherit_custom" model="ir.ui.view">
<field name="name">crm.lead.kanban.inherit.custom</field>
<field name="model">crm.lead</field>
<field name="inherit_id" ref="crm.crm_case_kanban_view_leads"/>
<field name="arch" type="xml">
<xpath expr="//div[hasclass('o_kanban_record_bottom')]" position="after">
<div class="oe_kanban_bottom_right o_kanban_bottom_extra">
<field name="quote_ids_ul"/>
<!-- <t t-set="record" t-value="record"/>
<li t-foreach="record.quote_ids" t-as="quote">
<p t-esc="quote.partner_id.name"/>
</li> -->
</div>
</xpath>
</field
您还可以使用 Odoo 小部件并避免创建计算字段...
<field name="quote_ids" widget="many2many_tags"/>