How to Inherit Model in Odoo

Abid Patel
10-Oct-2024 Updated : 10-Oct-2024

Learn how to inherit models in Odoo for customization. Extend and enhance existing Odoo models with classical and extension inheritance examples.

How to Inherit Model in Odoo

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.

Understanding Model Inheritance in Odoo

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.

1. Classical Inheritance in Odoo

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:

python

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

2. Extension Inheritance in Odoo

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:

python

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.

3. Inheriting Views in Odoo

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:

xml

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

Why Use Inheritance in Odoo?

There are several benefits to using inheritance in Odoo:

  • ▹ Maintain Core Integrity: By inheriting models, you avoid directly modifying Odoo’s core files, which means you can update Odoo without losing your customizations.
  • ▹ Extend Functionality: Inheritance allows you to add fields, methods, and views to an existing model, making it easy to adapt Odoo to your specific business needs.
  • ▹ Reusability: Inheritance promotes code reusability by allowing developers to extend models without rewriting the entire logic from scratch.

Conclusion

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.

Make a Comment

Your email address will not be published. Required fields are marked *