You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo产品页添加Map Price字段报错:无法定位barcode元素

Odoo视图继承报错:无法定位barcode元素的解决方法

问题详情

我试图在Odoo产品页面的Barcode字段后添加Map Price字段,编写的XML视图继承代码如下:

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
        <record id="product_template_map_price_label" model="ir.ui.view">
            <field name="name">product.template.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="//*[@name='barcode']" position="after">
                    <div class="map-price" groups="base.group_user">
                        <span class="oe_currency_value" name="map_price" groups="base.group_user"/>
                        <label for="map_price" groups="base.group_user">Map Price:</label>
                    </div>
                </xpath>
            </field>
        </record>
    </data>
</odoo>

执行后始终收到报错:

Element '<xpath expr="//*[@name='barcode']">' cannot be located in parent view

尝试更换多个XPath表达式仍未解决,从截图可见:

  • Barcode字段位于**库存(Inventory)**标签页下的嵌套分组内,完整XML路径为//form/sheet/notebook/page[@name='inventory']/group/group/field[@name='barcode']
  • 字段层级结构:表单 → 工作表 → 笔记本组件 → 库存标签页 → 分组 → 子分组 → barcode字段

解决方案

1. 使用精确XPath定位

通用的//*[@name='barcode']可能因视图层级或同名元素冲突无法匹配,改用更精准的路径:

简化精准路径(推荐)

<xpath expr="//page[@name='inventory']//field[@name='barcode']" position="after">
    <div class="map-price" groups="base.group_user">
        <label for="map_price" groups="base.group_user">Map Price:</label>
        <span class="oe_currency_value" name="map_price" groups="base.group_user"/>
    </div>
</xpath>

完全匹配截图路径

<xpath expr="//form/sheet/notebook/page[@name='inventory']/group/group/field[@name='barcode']" position="after">
    <div class="map-price" groups="base.group_user">
        <label for="map_price" groups="base.group_user">Map Price:</label>
        <span class="oe_currency_value" name="map_price" groups="base.group_user"/>
    </div>
</xpath>

注意:调整了label和字段的顺序,符合Odoo默认UI布局规范(先标签后字段)

2. 检查视图继承顺序

如果有其他模块先继承并修改了product.product_template_form_view中barcode字段的位置,需确保你的模块在这些模块之后加载:

  • 在模块的__manifest__.py中,将相关依赖模块添加到depends列表
  • 调整模块安装顺序,确保你的模块后于修改barcode的模块安装

3. 验证Map Price字段存在性

确保已通过Python代码将map_price字段添加到product.template模型:

from odoo import models, fields

class ProductTemplate(models.Model):
    _inherit = 'product.template'
    map_price = fields.Monetary(string='Map Price', currency_field='currency_id')

内容的提问来源于stack exchange,提问作者Lubomir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:35:23