DEV Community

Thalen Viremont Viremont
Thalen Viremont Viremont

Posted on

What causes a 'View Error: Element not found' during XML view inheritance using XPath in Odoo?"

This error typically occurs when the XPath expression used in the XML view does not match any element in the target view. Odoo fails to locate the element you are trying to extend, so the system throws aView error: Element not found.

Step-by-Step Solution:

1. Understand the View You're Inheriting:

Before writing an XPath expression, inspect the original view you're modifying. You can do this in Odoo:

  • Activate Developer Mode
  • Go to the form or tree view → Click "View → Edit View: Form/Tree"
  • Review the XML to see available field names or structure Problematic Code Example:
<record id="inherit_res_partner_form" model="ir.ui.view">
    <field name="name">res.partner.form.inherit</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='non_existing_field']" position="after">
            <field name="custom_field"/>
        </xpath>
    </field>
</record>
Enter fullscreen mode Exit fullscreen mode

Issue: non_existing_field does not exist in the inherited view, so Odoo cannot apply the modification.
Corrected Code Example:

<record id="inherit_res_partner_form" model="ir.ui.view">
    <field name="name">res.partner.form.inherit</field>
    <field name="model">res.partner</field>
    <field name="inherit_id" ref="base.view_partner_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='phone']" position="after">
            <field name="custom_field"/>
        </xpath>
    </field>
</record>
Enter fullscreen mode Exit fullscreen mode

Fix: Here, we’re inserting after the phone field, which is known to exist in the original view.
Odoo customization, the 'ViewError: Element not found' typically occurs during XML view inheritance when the XPath expression fails to locate the target element within the original view. This can happen due to incorrect model inheritance, misdefined field names, or an improper XPath that doesn’t align with the view structure. Odoo’s customization framework allows for modifications at the model and view levels, using Python for model overrides and XML for view adjustments. Resolving this error requires a thorough understanding of Odoo’s ORM, the XML structure of the base view, and the correct use of XPath expressions to ensure precise inheritance and customization.

Top comments (0)