Enable Dark Mode!
what-are-the-different-types-of-inheritance-in-odoo-18.jpg
By: Shafna K

What are the Different Types of Inheritance in Odoo 18

Technical

In Odoo, inheritance allows developers to extend, modify, or customize existing models, views, and functionalities without altering the core system. This modular approach ensures that customizations can be maintained even after upgrades. There are three main types of inheritance in Odoo: Python Inheritance, Model Inheritance, and View Inheritance. Each serves a specific purpose, making Odoo flexible for various business needs.

* Python Inheritance involves extending or overriding model methods and business logic.

* Model Inheritance includes Classical, Extension, and Delegation inheritance, allowing existing models to be extended or linked with new fields and functionalities.

* View Inheritance allows customizing the user interface without altering the original views by using XPath.

1. Python Inheritance

In Odoo, Python inheritance allows you to extend or modify existing model methods without rewriting them completely. Every model automatically inherits from the base models.Model class, providing access to basic CRUD methods (Create, Read, Update, Delete).

Example:

from odoo import models, api
class TestModel(models.Model):
    _name = "test_model"
    _description = "Test Model"
    @api.model
    def create(self, vals):
        # Add custom logic before creating the record
        return super(TestModel, self).create(vals)

In this example, the create() method is extended to add custom logic before the record is created. By using super(), the original create() method from the parent class is called.

2. Model Inheritance

Model inheritance in Odoo allows developers to extend or link models to add new fields or modify functionalities.

What are the Different Types of Inheritance in Odoo 18-cybrosys

There are three types of model inheritance:

a. Classical Inheritance

This type of inheritance allows a new model to inherit fields, methods, and metadata from an existing model, adding or modifying functionality. It uses both _name and _inherit.

Example:

from odoo import fields, models
class Animal(models.Model):
    _name = 'zoo.animal'
    _description = 'Animal'
    name = fields.Char(string='Animal Name')
    def sound(self):
        return f"{self.name} makes a sound!"
class Dog(models.Model):
    _name = 'zoo.dog'
    _inherit = 'zoo.animal'  # Classical inheritance
    _description = 'Dog'
    def sound(self):
        return f"{self.name} barks!"

Here, the Dog model inherits from the Animal and overrides the sound method.

b. Extension Inheritance

This is used to extend an existing model without creating a new one by adding fields or modifying methods using _inherit without _name.

Example:

from odoo import fields, models
class ExtendedBook(models.Model):
    _inherit = 'library.book'
    author = fields.Char(string='Author', default="Unknown Author")

In this example, the ExtendedBook model adds an author field to the library.book model.

c. Delegation Inheritance

Delegation inheritance allows a model to have fields from another model via a relationship. The child model doesn't inherit methods but can access fields through delegation using _inherits.

Example:

from odoo import fields, models
class ProductTemplate(models.Model):
    _name = "product.template"
    _description = "Product Template"
    name = fields.Char(string='Product Name', required=True)
    list_price = fields.Float(string='Sale Price')
class ProductProduct(models.Model):
    _name = "product.product"
    _inherits = {'product.template': 'product_tmpl_id'}  # Delegation inheritance
    product_tmpl_id = fields.Many2one('product.template', required=True, ondelete="cascade")
    sku = fields.Char(string='SKU')

In this case, ProductProduct inherits the fields from ProductTemplate via the product_tmpl_id field, but it doesn't inherit the methods or behavior of ProductTemplate.

3. View Inheritance

View inheritance allows you to modify existing views in Odoo without altering them directly. This is done using XPath expressions to target specific elements in the original view and either modify or extend them.

Example:

<record id="inherited_model_view_form" model="ir.ui.view">
    <field name="name">inherited.model.form.inherit</field>
    <field name="model">inherited.model</field>
    <field name="inherit_id" ref="base.model_view_form"/>
    <field name="arch" type="xml">
        <xpath expr="//field[@name='description']" position="after">
            <field name="new_field"/>
        </xpath>
    </field>
</record>

In this example, a new field new_field is added to a form view after the description field using XPath. This method ensures the original view remains intact and upgradable.

Odoo's inheritance mechanisms—Python, Classical, Extension, Delegation, and View Inheritance—offer powerful tools for extending both backend logic and frontend views. By leveraging these types of inheritance, developers can customize Odoo in a way that maintains compatibility with future upgrades and ensures modular, maintainable code.

To read more about What are the Different Types of Inheritance in Odoo 17, refer to our blog What are the Different Types of Inheritance in Odoo 17.


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



0
Comments



Leave a comment

 


whatsapp_icon
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