different-types-of-inheritance-odoo.png
Blogger_636313953779118730.jpg
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.

different-types-of-inheritance-odoo-cybrosys

1. Classical Inheritance
In this inheritance, we use both _name and _inherit attributes together. 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 check how this will work. For the time being, we are going to create records in 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. 

From this, we understand that all the functions, fields in the parent model can be accessed from the child model. Here the parent models fields, methods, etc did not change instead we used the parent models features without affecting its functionality.



2. Extension
Here we will use the only _inherit, not use the _name attribute. The child model replaces the existing one. This inheritance is used when we want to add new fields or methods to the existing model.

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")

Here when we check the database we can see the model containing the newly created fields also ‘ie now the fields in the model will be name and description.

3. Delegation
In delegation inheritance, we use the _inherits attribute. This is used if you want to sink another model in your current model without affecting the views. So the database tables contain fields in your model and also a field representing the inherited object.

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. 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



cybrosys youtube

Comments

0


Leave a comment

 
Calicut

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

London

Cybrosys Limited
Alpha House,
100 Borough High Street, London,
SE1 1LB, United Kingdom

Kochi

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

Bangalore

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

Send Us A Message