Odoo 15:产品表单新增电商标签页并迁移网站模块字段的问题
Odoo 15 产品表单迁移电商字段到新标签页解决方案
问题根源
- Odoo视图继承无法修改其他视图中的
xpath指令节点,因为xpath是视图继承的操作指令,不属于最终渲染的页面元素。 - 若继承
website_sale的视图,此时你新增的标签页尚未被加载(视图加载顺序按模块依赖和优先级),自然无法定位到目标标签页。
简洁可行方案
1. 模块依赖配置
确保你的模块__manifest__.py中添加依赖,保证website_sale视图先于你的视图加载:
'depends': ['product', 'website_sale'],
2. 保留新增E-Commerce标签页的代码
<record id="product_ecom_tab" model="ir.ui.view"> <field name="name">Product Ecom Tab Form</field> <field name="model">product.template</field> <field name="inherit_id" ref="product.product_template_form_view"/> <field name="arch" type="xml"> <xpath expr="//page[@name='sales']" position="after"> <page name="product_ecom" string="E-Commerce"> </page> </xpath> </field> </record>
3. 创建字段迁移视图(继承基础产品视图)
直接基于product.product_template_form_view继承,将website_sale添加的字段移动到新标签页,并整理布局、隐藏原位置残留元素:
<record id="product_ecom_fields_move" model="ir.ui.view"> <field name="name">Product Ecom Fields Move</field> <field name="model">product.template</field> <field name="inherit_id" ref="product.product_template_form_view"/> <field name="arch" type="xml"> <!-- 移动电商相关字段到新标签页 --> <xpath expr="//field[@name='website_published']" position="move"> <xpath expr="//page[@name='product_ecom']" position="inside"/> </xpath> <xpath expr="//field[@name='website_sequence']" position="move"> <xpath expr="//page[@name='product_ecom']" position="inside"/> </xpath> <xpath expr="//field[@name='website_description']" position="move"> <xpath expr="//page[@name='product_ecom']" position="inside"/> </xpath> <xpath expr="//field[@name='website_meta_title']" position="move"> <xpath expr="//page[@name='product_ecom']" position="inside"/> </xpath> <xpath expr="//field[@name='website_meta_description']" position="move"> <xpath expr="//page[@name='product_ecom']" position="inside"/> </xpath> <xpath expr="//field[@name='website_meta_keywords']" position="move"> <xpath expr="//page[@name='product_ecom']" position="inside"/> </xpath> <!-- 分组整理字段,优化界面 --> <xpath expr="//page[@name='product_ecom']" position="inside"> <group name="ecom_publish" string="发布设置"> <field name="website_published"/> <field name="website_sequence"/> </group> <group name="ecom_meta" string="SEO元数据"> <field name="website_meta_title"/> <field name="website_meta_description"/> <field name="website_meta_keywords"/> </group> <group name="ecom_description" string="网站描述"> <field name="website_description" widget="html"/> </group> </xpath> <!-- 隐藏原位置的电商字段分组(若存在) --> <xpath expr="//group[hasclass('oe_website_sale')]" position="attributes"> <attribute name="invisible">1</attribute> </xpath> </field> </record>
方案说明
- 所有操作基于基础产品视图,确保新标签页已被加载后再移动字段,避免定位失败。
- 使用
position="move"直接迁移字段,无需修改原website_sale的视图代码,符合Odoo视图继承的最佳实践。 - 分组整理字段让新标签页界面更清晰,同时隐藏原位置的空分组,避免界面冗余。
内容的提问来源于stack exchange,提问作者Steff
相关产品推荐
相关产品推荐

