When considering the features of Odoo, modularity is an important feature. Although each module is focused on a certain business purpose, they can also communicate with one another. This is helpful for increasing a module's capabilities. There are numerous circumstances during module modification where we must inherit certain models in order to accomplish a specific case. So in this blog, let us discuss the different types of inheritance in Odoo.
Different types of inheritance in odoo 15 include
1. Model Inheritance
2. View Inheritance
Model Inheritance
Primarily, Odoo offers three types of model inheritance. Which are Classical, Extension, and Delegation Inheritance.
1.1 Classical Inheritance
The first kind of inheritance is the classical inheritance, which combines the _name and _inherit properties. The new models also receive all of their bases' fields and methods.
Example:
class Inheritance Parent(models.Model):
_name = 'inheritance.parent
_description = 'Inheritance Parent
name = fields.Char(string="Name")
def test(self):
return self.test_check("model Parent")
def test_check(self, s):
return "This is a test check with {}".format(s)
class InheritanceChild(models.Model):
_name = 'inheritance.child’
_inherit = 'inheritance.parent’
_description = 'Inheritance Child’
def test(self):
return self.test_check("model child")
Usually, we can determine how this will function. The following codes can be used to create records in both models, which is what we'll do for the time being. The tests for both models are shown below:
a = env['inheritance.parent].create({'name': 'TEST A'})
a.test()
The output will be
This is a test check with model Parent.
b = env['inheritance.child').create({'name': 'TEST B'})
b.test()
The output will be
This is a test check with the model child.
Furthermore, it is clear that the child model can access all of the parent model's fields and functionalities. Additionally, we utilized the parent model's features in this case rather than changing its fields, methods, etc., without impairing its operation.
1.2 Extension
We shall only use the _inherit attribute and not the _name attribute when using the extension kind of inheritance. Additionally, the new model replaces the old one. This inheritance is employed when we want to expand the current model by including additional fields or functions. Here is an illustration of a command that can be used to add extended inheritance:
class ExtensionParent(models.Model):
_name = 'extension.extension’
_description = 'Extension”
name = fields.Char(default="I AM EXTENSION")
class ExtensionChild(models.Model):
_inherit = 'extension.extension'
description = fields. Char(default="Extended")
When we check the database, we can see that the model contains the newly added fields, namely Name, and Description.
1.3 Delegation
For delegation inheritance, we make use of the _inherits attribute. Additionally, you can sink another model to your current model using this form of inheritance without altering the views. Consequently, your model's fields and a field for the inherited object will both be present in the database tables. An example of a command that can be used to establish delegation inheritance is provided below:
Example:
class ProductTemplate(models.Model):
_name = "product.template"
class ProductProduct(models.Model):
_name = "product.product"
_inherits = {'product.template': 'product_tmpl_id'}
product_tmpl_id = fields.Many2one('product.template', 'Product Template', required=True, ondelete="cascade')
We can see that the product model includes many2one fields with a product template here. Odoo provides various examples for this. Although the Product model can access the fields from the Product template, such fields are only saved in the Product template table.
2. View Inheritance
Odoo provides view inheritance, which applies children's "extension" views on top of root views rather than replacing already-existing views (by overwriting them). The content of their parent view can be added or removed by these extensions. The inherit id attribute in an extension view serves as a parent reference. Its arch field comprises a number of XPath components that choose and modify the content of their parent view rather than a single view:
<record model="ir.ui.view" id="inherited_model_view_form">
<field name="name">inherited.model.form.inherit.test</field>
<field name="model">inherited.model</field>
<field name=”inherit_id” ref=”inherited.inherited_model_view_form”/>
<field name="arch" type="xml">
<!-- find field description and add the field
new_field after it -->
<xpath expr=”//field[@name=’description’]” position=”after”>
<field name=”new_field”/>
</xpath>
</field>
</record>
expr
A single element from the parent view is chosen via an XPath expression. it will produce error messages if it matches either no items or more than one
position
It is a procedure to apply to the selected element:
inside
The XPath's body is added at the end of the selected element.
replace
It replaces the selected element with the body or content provided in XPath
before
The XPath’s body is inserted before the selected element as a sibling
after
The XPath’s body is inserted after the selected element as a sibling.
attributes
It changes the attributes of selected elements using the specific attribute elements in the body of XPath.
If it matches only a single element, the position attribute can be set directly on an element. The same result will be obtained in both inheritances, as shown below.
<xpath expr=”//field[@name=”description] position=”after”>
<field name=”idea_ids”/>
</xpath>
<field name=”description” position=”after”>
<field name=”idea_ids”/>
</field>
Inheritance is extensively used in Odoo due to its modular concept. Multiple inheritances in odoo 15 have been discussed here. When customizing an Odoo module, Understanding how to inherit fields is quite helpful.