Odoo is known for being modular and flexible, giving a developer the ability to easily customize and extend existing functionality. One of the key features responsible for such flexibility is inheritance. Inheritance gives the ability to extend the existing models by reusing some or all of their fields and methods without reimplementing or duplicating code. Besides the most well-known classical inheritance with _inherit, another powerful mechanism exists, called delegation inheritance, which is implemented using _inherits.
Although this sounds very similar to classical inheritance, it actually serves a different purpose and works quite differently under the hood. So let's learn what delegation inheritance is, how it works, and when to use it in Odoo 19.
What is Delegation Inheritance?
Delegation inheritance means that one model reuses the fields from another model through a relationship, rather than directly inheriting it as a subclass. In other words, it ties two models together in such a way that the child model gains access to the parent model's fields, but both models retain their own separate database tables.
This means that when delegation inheritance is used, the child model does not merge into the parent model's table but links with it through a Many2one relationship field. That enables you to read and write data on the parent model as if those fields belonged to the child model.
How Delegation Inheritance Works
For delegation inheritance, Odoo uses the _inherits attribute to define the relationship. It expects a dictionary where the key is the name of the model to inherit from, and the value is the field name used to link to it. The linking field must be a Many2one relation to the parent model.
Whenever you access a field from the parent model, Odoo automatically delegates the call through this relationship. The result of this is that the child model appears to possess all the fields of the parent, even though they are really stored in different tables.
Example: Delegation Inheritance in Action
Let's illustrate this with a more practical example, using product models
from odoo import fields, models
class ProductTemplate(models.Model):
_name = "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'}
product_tmpl_id = fields.Many2one('product.template', required=True, ondelete="cascade")
sku = fields.Char(string='SKU')
This setup is ideal when you want to extend or reuse an existing model's data but still maintain an independent table for your new model.
When to Use Delegation Inheritance
Delegation inheritance is particularly helpful when you want to:
- Reuse fields from another model without merging tables.
- Keep your new model's data independent.
- Extend capabilities without touching the parent's structure.
- Maintain clear separation between business objects while sharing information.
Conclusion
Delegation inheritance is flexible, reusing existing models' fields and relationships without altering each model's database schema. Relating models to one another rather than merging them allows developers to achieve better control, modularity, and readability in their applications. If used wisely, inheritance will be a key tool for creating maintainable and scalable Odoo modules, making it easy to interact with other modules without having to dive into their code.
To read more about What are the Different Types of Inheritance in Odoo 18, refer to our blog What are the Different Types of Inheritance in Odoo 18.