In Odoo, view inheritance is a powerful mechanism that enables
developers to extend or modify existing views without altering their
original source code. This approach is especially valuable for
customizing standard features while preserving upgrade compatibility
and code integrity. View inheritance is widely used in custom
modules to apply changes to views that are originally defined in
other modules, promoting modularity and maintainability.
<record model="ir.ui.view" id="sale_margin_sale_order_line">
<field name="name">sale.order.view.form.inherit.hospital.management</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='payment_term_id']" position="after">
<field name="margin"/>
</xpath>
</field>
</record>
The above code represents a customization of the Sale Order form view
in Odoo. It introduces a new field, “margin”, into the form view of
the “sale.order” model. This custom view, identified as
“sale.order.view.form.inherit.hospital.management”, inherits from
the existing view with the ID “sale.view_order_form“. Using an XPath
expression, the “margin” field is inserted immediately after the
“payment_term_id” field within the form layout. Additionally, Odoo
supports the use of multiple XPath expressions in XML view
customizations, allowing developers to modify various parts of the
view structure with great flexibility.
Some examples are
expr="//field[@name='user_id']"
expr="//sheet/div[last()]"
expr="/kanban"
expr="."
expr="//filter[@name='activities_overdue']
expr="//header"
expr="//button[@name='action_open_product_lot']"
Furthermore, it is possible to specify different positions for XPath
expressions in Odoo XML view customizations, enabling precise
control over where elements are inserted or modified within a view.
The available position options include:
- inside – Inserts the content within the targeted element.
- before – Places the content immediately before the
targeted element.
- after – Places the content immediately after the targeted
element.
- replace – Replaces the targeted element entirely with new
content.
- attributes – Modifies or adds attributes to the targeted
element without changing its structure.
These position options provide powerful flexibility for tailoring
views to meet specific functional or layout requirements without
altering the original base views.