Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 39 ยท Deployment and Customisation

Customising with View and Model Inheritance

Customise Odoo without editing core code: inherit views with xpath and position, extend models with _inherit, add fields to existing apps, and know when to use Studio or code.

The golden rule of Odoo customisation is: never edit the standard modules. The next upgrade would overwrite your change, and you could not tell what you had altered. Instead you write a small module that inherits: it extends models in Python and patches views in XML. This is why the same Odoo can serve a bakery and a shipping company.

Odoo code here is read-only. The stand-in at the end applies xpath patches to an XML view in Python so you can see exactly what each position does.

Extending a model

Python
from odoo import fields, models


class ResPartner(models.Model):
    _inherit = "res.partner"          # same _name: extends the existing model in place

    library_card = fields.Char(string="Library card")
    is_member = fields.Boolean(compute="_compute_is_member", store=True)

    def _compute_is_member(self):
        for partner in self:
            partner.is_member = bool(partner.library_card)

With _inherit and no new _name, the model is extended: the table gets new columns and every other module sees the new fields. You can also override a method and call super():

Python
    def write(self, vals):
        if "library_card" in vals:
            vals["library_card"] = (vals["library_card"] or "").strip().upper()
        return super().write(vals)

Other inheritance styles exist, and mixing them up is a common beginner trap:

Style How Effect
Extension _inherit = "model" Adds to the same model and table
Classical _name = "new" + _inherit = "old" A new model that copies the old one's fields, with its own table
Delegation _inherits = {"res.partner": "partner_id"} A new model that exposes the parent's fields through a link, so rows are split across two tables
Mixin _inherit = ["mail.thread"] Adds reusable behaviour like chatter

Inheriting a view

You put the change in a view record of your own module with inherit_id pointing at the original:

XML
<record id="view_partner_form_library" model="ir.ui.view">
  <field name="name">res.partner.form.library</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='website']" position="after">
      <field name="library_card"/>
    </xpath>

    <field name="phone" position="attributes">
      <attribute name="required">1</attribute>
    </field>

    <xpath expr="//page[@name='sales_purchases']" position="inside">
      <group string="Library">
        <field name="is_member"/>
      </group>
    </xpath>

  </field>
</record>

Each patch has an expression that finds a node and a position that says what to do:

position Result
before / after Insert the contents next to the matched node
inside Append to the end of the matched node
replace Replace the node (empty content removes it)
attributes Change attributes with <attribute name="x">value</attribute>
move Move another node here (inside an xpath)

The shorthand <field name="phone" position="attributes"> finds the first field with that name, equivalent to //field[@name='phone']. Use xpath when you need pages, groups or buttons, matched by a stable attribute such as @name. A button with a name is easier to match than one picked by position, so write names on your own elements to help future extensions.

flowchart TD
    B["Base view<br/>base.view_partner_form"] --> M["Merge extension 1<br/>(module A, priority 16)"]
    M --> N["Merge extension 2<br/>(module B, priority 20)"]
    N --> F["Final arch<br/>sent to the browser"]

Extensions apply in priority order on top of the original; the combined result is what users see.

Why inheritance can break

  • Missing node. If the base view changes and your xpath no longer matches, installing or updating fails with an "element not found" error. This is a good failure; it tells you the module needs porting during an upgrade.
  • Order matters. If extension B depends on a node added by A, give it a higher priority or depend on A's module in the manifest.
  • Fragile expressions. //group[3]/field[2] breaks easily. Prefer @name.
  • Overriding too much. Replacing a whole block copies standard code into yours; later fixes never reach you.

Also remember to add the new fields to your module's depends list: your module must depend on the module that defines base.view_partner_form (here base, or contacts for the Contacts app) or loading fails.

Studio and low-code customisation

On Odoo Enterprise, Studio lets administrators add fields, rearrange forms, create simple models, automations and reports from the browser. It stores the changes as inheritance records in a generated module, so everything above still applies underneath. Choose between them like this:

  • Studio for quick field additions and layout changes by administrators.
  • A custom module for logic, performance-sensitive code, anything that needs version control, tests or review.
  • Do not mix both on the same view without a plan: Studio edits to a view that your module also modifies can conflict.

Patching an arch in Python

The stand-in below applies after, inside, attributes and replace to a form, using xml.etree.ElementTree and a simple expression of the form .//tag[@name='value']. Odoo's real engine uses lxml and full XPath, which supports far more.

Python Apply inheritance patches to a view

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A real inheritance chain applies several extension views in order. This last stand-in checks that order and dependencies decide which patch finds its target.

Python Ordering extensions by priority

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens with _inherit = "res.partner" and no new _name?

    Extension inheritance adds fields and methods to the same model and table, visible to every module.
  2. 2. Which position appends content at the end of the matched element?

    inside puts the new content inside the matched node, after its existing children.
  3. 3. Why is an xpath like //group[3]/field[2] a bad idea?

    Positional paths depend on the exact layout, while named elements survive layout changes.
  4. 4. Why should you never edit a standard module directly?

    Customisations belong in your own inheriting module, so upgrades stay possible.

Summary

  • Extend models with _inherit (same name = in place); override methods and call super().
  • Inherit views with inherit_id, an xpath expression and a position: before, after, inside, replace, attributes or move.
  • Match stable @name attributes, set the right depends and mind priority order.
  • A failed xpath is a useful signal during upgrades.
  • Studio is fine for light changes; use modules for code you must review and version.

Next: Project: A Library Module