Chapter 13: Inheritance
A powerful aspect of Odoo is its modularity. A module is dedicated to a business need, but modules can also interact with one another. This is useful for extending the functionality of an existing module. For example, in our real estate scenario we want to display the list of a salesperson’s properties directly in the regular user view.
But before going through the specific Odoo module inheritance, let’s see how we can alter the behavior of the standard CRUD (Create, Retrieve, Update or Delete) methods.
In our real estate module, we never had to develop anything specific to be able to do the standard CRUD actions. The Odoo framework provides the necessary tools to do them. In fact, such actions are already included in our model thanks to classical Python inheritance:
These methods (and any other method defined on
Model) can be extended to add specific business logic:
It is also important to note that even though we can directly override the
unlink() method, you will almost always want to write a new method with the decorator
ondelete() instead. Methods marked with this decorator will be called during
unlink() and avoids some issues that can occur during uninstalling the model’s module when
unlink() is directly overridden.
In Python 3,
super() is equivalent to
super(TestModel, self). The latter may be necessary when you need to call the parent method with a modified recordset.
Reference: the documentation related to this topic can be found in Inheritance and extension.
In our real estate module, we would like to display the list of properties linked to a salesperson directly in the Settings / Users & Companies / Users form view. To do this, we need to add a field to the
res.users model and adapt its view to show it.
Odoo provides two inheritance mechanisms to extend an existing model in a modular way.
The first inheritance mechanism allows modules to modify the behavior of a model defined in an another module by:
adding fields to the model,
overriding the definition of fields in the model,
adding constraints to the model,
adding methods to the model,
overriding existing methods in the model.
The second inheritance mechanism (delegation) allows every record of a model to be linked to a parent model’s record and provides transparent access to the fields of this parent record.
In Odoo, the first mechanism is by far the most used. In our case, we want to add a field to an existing model, which means we will use the first mechanism. For example:
A practical example where two fields are added to a model can be found here.
By convention, each inherited model is defined in its own Python file. In our example, it would be
Now let’s add the field to the view and check that everything is working well!
Reference: the documentation related to this topic can be found in Inheritance.
Instead of modifying existing views in place (by overwriting them), Odoo provides view inheritance where children ‘extension’ views are applied on top of root views. These extension can both add and remove content from their parent view.
An extension view references its parent using the
inherit_id field. Instead of a single view, its
arch field contains a number of
xpath elements that select and alter the content of their parent view:
An XPath expression selecting a single element in the parent view. Raises an error if it matches no element or more than one
Operation to apply to the matched element:
xpath’s body to the end of the matched element
replaces the matched element with the
xpath’s body, replacing any
$0node occurrence in the new body with the original element
xpath’s body as a sibling before the matched element
xpaths’s body as a sibling after the matched element
alters the attributes of the matched element using the special
attributeelements in the
When matching a single element, the
position attribute can be set directly on the element to be found. Both inheritances below have the same result.
An example of a view inheritance extension can be found here.
Inheritance is extensively used in Odoo due to its modular concept. Do not hesitate to read the corresponding documentation for more info!
In the next chapter, we will learn how to interact with other modules.