Odoo14如何通过继承自定义产品模板视图及修复代码不生效问题
问题排查与修正方案
1. 核心问题原因
- 模型定义逻辑错误:你同时声明了
_name = "mymodule.product"和_inherit = "product.template",属于新建独立模型而非扩展原生的product.template模型,你修改的是新建模型的表单视图,原生产品模板页面自然不会加载自定义内容。 - 视图参数错误:xpath的
position="attributes"参数仅用于修改目标节点的属性,无法用来新增子字段,不符合你插入自定义字段的需求。
2. 修正步骤
第一步:修正模型定义
如果需求是给原生产品模板新增字段,直接删除_name声明即可:
class MyModuleProduct(models.Model): _description = "table des articles" _inherit = "product.template" prixass = fields.Float(string='Prix Assurance') taxes_id = fields.Many2many('account.tax', 'product_hospi_taxes', 'prod_hospi_id', 'tax_id', string='Customer Taxes') supplier_taxes_id = fields.Many2many('account.tax', 'product_hospi_supplier_taxes', 'prod_hospi_id', 'tax_id', string='Vendor Taxes') route_ids = fields.Many2many('stock.location.route', 'stock_route_product_hospi', 'product_hospi_id', 'route_id', string='Routes',)
第二步:修正视图代码
以下代码同时实现新增prixass字段、移除taxes_id字段的需求:
<!-- product template --> <record id="mymoduleproduct_view_form" model="ir.ui.view"> <field name="name">mymodule.product</field> <field name="model">product.template</field> <field name="inherit_id" ref="product.product_template_form_view"/> <field name="arch" type="xml"> <!-- 新增prixass字段到通用信息页的第二个分组内 --> <xpath expr="//notebook/page[@name='general_information']/group[2]" position="inside"> <field name="prixass"/> </xpath> <!-- 移除taxes_id字段,两种方案二选一 --> <!-- 方案1:直接移除节点(不需要保留字段逻辑选这个) --> <xpath expr="//field[@name='taxes_id']" position="remove"/> <!-- 方案2:设为隐藏(后续需要条件显示推荐用这个) --> <!-- <xpath expr="//field[@name='taxes_id']" position="attributes"> <attribute name="invisible">1</attribute> </xpath> --> </field> </record>
3. 生效操作
修改完成后升级对应模块,刷新页面即可看到效果。
内容的提问来源于stack exchange,提问作者Jhon
相关产品推荐
相关产品推荐

