
Learn how to inherit models in Odoo for customization. Extend and enhance existing Odoo models with classical and extension inheritance examples.
In Odoo, one of the most powerful features of its framework is the ability to inherit and extend existing models. This allows developers to customize and add functionality to standard models without modifying the core codebase. By using the inheritance feature in Odoo, you can maintain system stability while implementing necessary customizations for your business requirements. In this blog, we will explain how to inherit a model in Odoo with step-by-step instructions and examples.
Odoo supports two types of inheritance: classical inheritance and extension inheritance. With classical inheritance, you can directly inherit from a model and override its methods. With extension inheritance, you can extend an existing model without modifying the original model’s behavior by using the _inherit
attribute. Let’s go over both approaches.
With classical inheritance, you can fully inherit the behavior of an existing model and change it as per your needs. Here's an example of classical inheritance in Odoo:
from odoo import models, fields
class CustomSaleOrder(models.Model):
_inherit = 'sale.order'
custom_field = fields.Char(string="Custom Field")
def action_confirm(self):
# Override the action_confirm method
res = super(CustomSaleOrder, self).action_confirm()
# Add custom functionality
print("Sale Order Confirmed!")
return res
In this example, the model sale.order
is inherited using the _inherit
attribute. A new field called custom_field is added, and the action_confirm method is overridden to add custom logic while keeping the original behavior intact by calling super()
.
Extension inheritance is useful when you want to add additional fields or methods to an existing model without fully overriding the class. You can achieve this by using both _name
and _inherit
attributes together. Here’s an example:
from odoo import models, fields
class CustomPartner(models.Model):
_name = 'res.partner'
_inherit = 'res.partner'
custom_partner_field = fields.Char(string="Custom Partner Field")
In this case, we extend the res.partner model by adding a new field custom_partner_field. The original behavior of the model is not changed, and the model’s functionality is enhanced with the new field.
Odoo allows developers to inherit and extend not only models but also views. This is done using the inherit_id
attribute in XML. Here’s an example of how to inherit a view in Odoo:
<record id="view_partner_form_custom" model="ir.ui.view">
<field name="name">res.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='name']" position="after">
<field name="custom_partner_field"/>
</xpath>
</field>
</record>
In this XML example, we inherit the res.partner form view and add a custom field custom_partner_field after the name field using the inherit_id attribute. This way, you can extend existing views without disrupting the original form layout.
There are several benefits to using inheritance in Odoo:
Inheriting models in Odoo is an essential feature for developers looking to customize the system while maintaining core stability. Whether you are using classical inheritance to override methods or extension inheritance to add fields, Odoo's framework provides the flexibility you need to build robust, customized solutions. By following the examples provided above, you can start implementing inheritance in your Odoo modules effectively.
For more Odoo development tips and tricks, be sure to check our website Free web snippets.
Your email address will not be published. Required fields are marked *