Enable Dark Mode!
different-types-of-inheritance-odoo.png
By: Anusha

Different Types of Inheritance in Odoo

Technical

During module customization, there are many situations when we need to inherit some models in order to achieve a particular case.  Odoo supports 3 varieties of model inheritance primarily.
This blog will provide an insight into the technical aspects of the 3 different types of inheritance they Odoo supports.

different-types-of-inheritance-odoo-cybrosys
1. Classical Inheritance
The initial type of inheritance is the classic inheritance where we use both _name and _inherit attributes together. In addition, the new models get all methods, fields from its base.
Example:
class InheritanceParent(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")
We can usually check how this will work. However, for the time being we are going to create records in both models which can be done by using the following codes.
Here, are the test on both models:
        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. 
Moreover, it can be understood that all the functions, fields in the parent model can be accessed from the child model. In addition, here the parent models fields, methods, etc are not changed, instead we used the parent models features without affecting its functionality.
2. Extension
In the extension type of inheritance we will only use the _inherit, and do not use the _name attribute. Moreover, the child model replaces the existing one. In addition, this inheritance is used when we want to add new fields or methods to the existing model.
Here is an example of the command used to add the extension 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")
Furthermore, when we check the database now we can see the model containing the newly created fields i.e. now the fields in the model will be name and description.
3. Delegation
In the delegation inheritance, we use the _inherits attribute. In addition, this type of inheritance is used if you want to sink another model to your current model without affecting the views. Therefore, the database tables will contain fields in your model and also a field representing the inherited object.
Here is an example of the command used to add the delegation inheritance:
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")
In Odoo we have different examples, for that here we can see the product model has many2one fields with a product template. Moreover, the Product model can use the fields from the Product template but the fields in the product template are only stored in the product template table.

Odoo Blogs Odoo Development Tutorials Odoo 13 Book



If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message