How to override method in Odoo Model

In the realm of Odoo development, method overriding stands as a fundamental technique for extending and customizing the functionality of existing models and classes. By overriding methods, developers can modify the behavior of Odoo's built-in features, adding new functionalities or altering existing ones to meet specific business requirements. In this blog, we'll explore the concept of method overriding in Odoo, providing clear explanations along with code examples to illustrate its implementation.


Understanding Method Overriding in Odoo

Method overriding refers to the process of redefining a method from a parent class in a child class, thereby replacing the original implementation with a customized one. In the context of Odoo, method overriding allows developers to modify the behavior of predefined methods in models, controllers, and other components of the framework. This powerful technique enables developers to add new features, implement custom business logic, or alter default behaviors without modifying the core Odoo codebase.


Example 1: Method Overriding in Models

Let's consider a scenario where we want to override the create method of an existing model in Odoo to add custom validation logic before creating a new record. Here's how we can achieve this:


from odoo import models, fields, api

class CustomModel(models.Model):
    _inherit = 'base.model'

    @api.model
    def create(self, vals):
        # Custom validation logic
        if vals.get('field_to_validate') == 'invalid_value':
            raise ValueError("Invalid value for field_to_validate")
       
        # Call super to execute the original create method
        return super(CustomModel, self).create(vals)

In this example, we inherit the base.model and override its create method. We add custom validation logic to check if the value of a specific field meets certain criteria. If the validation fails, we raise an exception; otherwise, we call the original create method using super() to create the record.

Example 2: Method Overriding in Controllers

Now, let's explore how method overriding can be used in controllers to customize the behavior of route handlers. Suppose we want to customize the response returned by a controller method. Here's how we can do it:

from odoo import http

class CustomController(http.Controller):

    @http.route('/custom/route', type='http', auth='public')
    def custom_route(self, **kwargs):
        # Custom logic to modify response
        response = "Custom response message"
        
        # Return the customized response
        return response

In this example, we define a controller method custom_route and override its default behaviour to return a custom response message.


Conclusion

Method overriding is a powerful technique in Odoo development that empowers developers to extend and customize the functionality of existing components with ease. Whether it's models, controllers, or other parts of the framework, method overriding allows developers to tailor Odoo to meet the specific requirements of their projects. By mastering method overriding, developers can unlock endless possibilities for building custom solutions that deliver added value to businesses and users alike.

How to override method in Odoo Model
S S 9 May 2024
Share this post
Tags
Odoo Documentation