Inheritance in Odoo 15
In Odoo, modularity is an important feature. Where each module is focused on a specific goal, between modules can communicate with each other. There are many situations during modifying a module where one has to inherit a certain module to solve a certain case. So in this blog, let's discuss the different types of inheritance in Odoo.
There are two types of Odoo about Inheritance.
- Model Inheritance
- View Inheritance
1. Model Inheritance
Mainly, Odoo offers three types of model inheritance. Which are Classical, Extension, and Delegation Inheritance. But for now, explain Classical Inheritance first.
Classical Inheritance
Classical inheritance is one that combines the _name and _inherit properties. The new model also accepts all fields and their base methods.
For Example, create a new class.py and the class name is 'excredit' inside the model's folder and fill it with example code like:
from odoo import models,fields, api
class usersInherit(models.Model):
_inherit = 'res.users'
books_ids = fields.One2many(string="Books", comodel_name="library_basic.excredit", inverse_name="borrower")
Comodel_name is the Object relation name in field One2many, for example, the relation about code above in class ‘excredit’, and the ‘library_basic’ is the name of the model that comes from.
For more specific about relational field, click here.
_inherit = 'rest.users' defines the model you want to associate with a particular model
And also don’t forget to add the name of the model.py in __init__.py
For Example:
from.import inherit_user
After importing in __init__.py and continue to discuss the view inheritance below.
View inheritance
View inheritance is provided in odoo, which applies a child "extension" over the root view rather than overriding the existing one (by overwriting them). Display content from its parent can be added or removed from this extension. The inherited id attribute serves as the parent reference. The field consists of multiple XPath components that select and modify the contents of their parent view rather than a single view
For Example, put a dialog box in the Notebook, whats is Notebook?
Notebook refers to tab section, each tab is defined through a child element called pages. The main function of the notebook is to give a tabbed section in the form view, like:
After knowing what its Notebook There is a separate way in odoo to know where is code Notebook in odoo, the step is to go to the page of the model you want to inherit then select the bug icon at the top right if it is entered in developer mode, like:
After clicking on Edit View: Form it can show a dialog pop-up such as view name, view type, model, etc. but focus on Architecture below the form field. Because we want to know the position of the notebook on the code page of the form,
After finding and knowing the position just add some code in .xml class that defines a module that wants to inherit it.
For example, create a new class.xml and fill it with the example code:
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<record id="view_users_form" model="ir.ui.view">
<field name="name">view_users_form</field>
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook/page[1]" position="after">
<page name="books_ids" string="Books">
<field name="books_ids"/>
</page>
</xpath>
</field>
</record>
</odoo>
After adding the code don’t forget to add the class name in _manifest_.py inside 'data': [],
For Example:
'data': [
...
'views/inherit_user.xml', // class name from view
...
],
After everything has been added, an additional field will appear as an book's in the user's notebook form, such as:
Explanation about code in View inheritance:
expr
It will produce error messages if it matches either no items or more than one.
position
This is the procedure for applying a record to the selected element.
inside
The XPath body is used to add to the end of the selected elements.
replace
to replace the selected element with the content provided in the XPath.
before
The XPath’s body is inserted before the selected element.
after
The XPath’s body is inserted after the selected element.
attributes
To make changes to the attributes of the selected elements using specific attributes in the XPath body
Aligned with an element, the attribute position can be set directly on the element. the result will be obtained in both inheritance, and sample code as below:
<xpath expr="//notebook/page[1]" position="after">
<page name="books_ids" string="Books">
<field name="books_ids"/>
</page></xpath>
Odoo uses the concept of inheritance a lot because of its modular concept. the discussion about inheritance in odoo has been explained here. When customizing multiple modules in odoo, learning and understanding how to inheritance fields in odoo are very helpful.
To see the full code, visit the following page.
Conclusion
Inheritance is divided into two, there are those who view inheritance and model inheritance. each has a different function to view inheritance just change in the .xml section regarding the position and display what modules you want to inherit for the model inherit it defines the model you want to inherit and also gives the Relational fields about the name of the module for example 'Many2one, One2many and also Many2many'.