Odoo14 如何通过Selection选择框控制表单Notebook页面切换
Odoo14 Selection切换Notebook页面内容重复问题解决方案
问题根源
该问题由Odoo前端视图渲染的默认规则导致:同一个模型字段在同一张表单视图中多次出现时,Odoo会默认复用第一个该字段的视图配置,后续同名字段的自定义tree/form结构会被忽略。你的两个page都调用了同一个header_id字段,所以只会渲染第一个定义的Labour Cost的tree结构。
另外第一个page的invisible规则存在语法问题:not in的右值需要为列表,否则可能出现显隐控制不生效的异常。
解决方法
方案1:添加唯一context区分字段渲染实例(推荐,无需修改模型)
给每个page里的header_id字段添加独一无二的context参数,强制Odoo将其识别为不同的渲染实例,即可正常加载各自的tree配置,修改后的XML代码如下:
<notebook> <page string="Labour Cost" name="labour_cost" attrs="{'invisible': [('package_name', '!=', 'labour')]}"> <!-- 添加唯一context,同时修正了原not in的语法问题 --> <field name="header_id" context="{'cost_type': 'labour'}" attrs="{'invisible': [('package_name', '!=', 'labour')]}"> <tree string="Labour Cost Tree" editable="bottom"> <field name="cost_header_id"/> <field name="description"/> <field name="header_cost"/> </tree> </field> </page> <page string="Material Cost" name="material_cost" attrs="{'invisible': [('package_name', '!=', 'material')]}"> <!-- 添加和第一个page不同的context --> <field name="header_id" context="{'cost_type': 'material'}" attrs="{'invisible': [('package_name', '!=', 'material')]}"> <tree string="Material Cost Tree" editable="bottom"> <field name="product_id"/> <field name="qty"/> <field name="unit_price"/> <field name="uom"/> <field name="subtotal"/> </tree> </field> </page> </notebook>
修改后升级对应模块即可生效。
方案2:拆分不同业务字段(适合数据分开存储的场景)
如果不同成本类型的明细数据需要独立存储,可在模型中定义多个独立的One2many字段:
# 模型层新增多个独立字段示例 labour_header_ids = fields.One2many(...) material_header_ids = fields.One2many(...)
之后每个page调用对应名称的字段即可,不会出现渲染冲突。
内容的提问来源于stack exchange,提问作者Sidharth Panda
相关产品推荐
相关产品推荐

