Odoo’s view inheritance system is powerful — but for many developers, understanding how XPath expressions work when modifying views can feel intimidating. In this short guide, we’ll break it down to the essentials so you can confidently customize Odoo interface elements the right way.
What Is XPath in Odoo Views?
In Odoo, XPath is a way to target specific elements inside an existing XML view so you can modify them without rewriting the entire view.
Instead of copying the whole view and editing it (which is brittle and hard to maintain), you use XPath to say:
- Find this element
- Then do this operation
This makes your changes clean, precise, upgrade‑friendly, and reusable.
Where You Usually Use XPath
You’ll find XPath mostly inside:
- ir.ui.view inheritance definitions
- Inherited templates
- xpath tags inside your view inheritance
Example:
<record id="view_partner_form_inherit" 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='target_field_name']" position="after">
<field name="your_field"/>
</xpath>
</field>
</record>
Basic Anatomy of an XPath in Odoo
Let’s break down the key parts:
1. expr — The XPath Expression
This is the selector that tells Odoo what to find.
Example:
expr="//field[@name='phone']"
This means:
📍 Find a <field> tag with attribute name="phone" anywhere in the view XML.
2. position — What You Want to Do
Common values:
- replace → replace the selected element entirely
- after → insert content right after the element
- before → insert content right before the element
- inside → insert content inside the element
- attributes → modify attributes of the selected element
How XPath Works at Runtime
When Odoo loads a view:
- It finds the base view XML (the original form, tree, kanban, etc.)
-
It applies all inherited views in sequence
– Each inherited view uses XPath to modify or inject new parts - The final merged XML is sent to the client
This means:
- Your XPath doesn’t change the original view
- Instead, it decorates or adjusts it at runtime
- Multiple inherited views stack their changes
Conclusion
Understanding how XPath works in Odoo when inheriting views unlocks a huge part of the platform’s customization power. It allows you to modify interface elements cleanly without rewriting entire views — keeping your code modular, readable, and maintenance‑friendly.
Once you get comfortable with a few core XPath patterns, view inheritance becomes a tool, not a mystery.
