Odoo many2many复选框Domain不生效问题求助
问题
现有模型结构:
event.booth模型包含halle_id字段order.wizard模型包含event_booth_id(单关联字段)和event_booth_ids(多关联字段)
需求:在order.wizard的表单视图中,event_booth_ids字段使用many2many_checkboxes小部件时,仅显示halle_id与event_booth_id.halle_id匹配的记录。
当前视图配置代码:
<record id="rectanglemaps_create_order_view_form" model="ir.ui.view"> <field name="name">rectanglemaps.order.wizard.form</field> <field name="model">rectanglemaps.order.wizard</field> <field name="arch" type="xml"> <form> ... <field name="event_booth_ids" widget="many2many_checkboxes" domain="[('halle_id', '=', event_booth_id.halle_id)]"/> </form> </field> </record>
尝试使用domain="[('halle_id', '=', self.event_booth_id.halle_id)]"时出现错误:
Ungültiges zusammengesetztes Feld in domain="[('halle_id', '=', self.event_booth_id.halle_id)]"
(错误含义:域中存在无效的复合字段)
需要解决该Domain配置问题。
解决方案
方法1:修正视图域的字段引用方式
Odoo视图域不支持self关键字,直接使用当前模型的关联字段路径即可:
<field name="event_booth_ids" widget="many2many_checkboxes" domain="[('halle_id', '=', event_booth_id.halle_id)]"/>
若该写法仍报错,说明视图域不支持跨层级字段引用,可改用以下方法。
方法2:通过模型层的onchange方法动态更新域
在order.wizard模型中添加onchange方法,实时根据event_booth_id的变化更新event_booth_ids的可选范围:
from odoo import models, fields, api class OrderWizard(models.Model): _name = 'rectanglemaps.order.wizard' event_booth_id = fields.Many2one('event.booth', string='Event Booth') event_booth_ids = fields.Many2many('event.booth', string='Event Booths') @api.onchange('event_booth_id') def _onchange_event_booth_id(self): if self.event_booth_id: return { 'domain': { 'event_booth_ids': [('halle_id', '=', self.event_booth_id.halle_id)] } } else: return { 'domain': { 'event_booth_ids': [] } }
此方法能实现实时联动,event_booth_id变更时立即刷新event_booth_ids的可选列表。
方法3:在模型字段定义中设置动态域
直接在event_booth_ids字段定义中添加动态domain逻辑:
from odoo import models, fields class OrderWizard(models.Model): _name = 'rectanglemaps.order.wizard' event_booth_id = fields.Many2one('event.booth', string='Event Booth') event_booth_ids = fields.Many2many( 'event.booth', string='Event Booths', domain=lambda self: [('halle_id', '=', self.event_booth_id.halle_id)] if self.event_booth_id else [] )
内容的提问来源于stack exchange,提问作者unnamed-dev
相关产品推荐
相关产品推荐

