Home Blog How to Inherit a Function without a Class in Odoo
How to Inherit a Function without a Class in Odoo

How To Inherit a Function Without a Class In Odoo

In Odoo, one of the most loved open-source ERP frameworks, object-oriented programming plays a significant role in extending and customizing the system’s functionalities. One of the most fundamental concepts in odoo is the class inheritance, which further allows developers to modify existing methods or add new ones.

However , there could be scenarios where you would want to inherit a function without a class, now that would require a slightly different approach.

In our blog today, we will discuss how to inherit a function without a class in odoo.


For a better understanding, Let us first understand odoo’s function inheritance:

In default Odoo, the function inheritance is based on class-level inheritance. For example, You create a new class that extends an existing class, and then modify or add methods within that new class.

However, in certain cases, you might want to inherit a function without having a class. Now this condition may not show up in Odoo, but it exists and while developing I looked on the internet for the best and simple solution and that is when I learned about “monkey patching” technique.

It is a good technique, but still i looked for a simple solution, Now I'm going to tell you how you can bypass monkey technique with my solution:

Step1: Create a .py file 

Step2: Call this .py file in __init__.py

Step 3: Identify the target function:

First, identify the function you want to inherit. It could be a function defined in a module, an odoo base function, or even a custom function from another module.

Step 4: Now in your created .py file import the path of that function file i.e,

Like  I highlighted in the above-given image.

For better understanding I explained to you about the path and what to import.

In this given line import odoo.addons.portal.controllers.mail

                            ↑                          ↑

The default odoo addons path shows module name>folder controller>file mail(where our function without a class is written).

Step 5: Write your function in your created .py file with a new name or either with the same as the function you want to inherit. Like the given image.

Within your new function, implement the custom logic or modifications.You do not have to call the original function using the super() method to inherit its functionality and extend or modify it as required.

Step 6: Replace the Original Function like i did  in the below-given image, for better understanding let me explain what does that line means.

To wrap it up, understanding how to inherit a function without a class in Odoo would open up different possibilities for high-level customizations and extending functionalities.



While class inheritance remains a fundamental concept of Odoo, the sheer ability to inherit functions without a class provides developers with a much more flexible approach.

Contact us

Get In Touch with Us

Protected by reCAPTCHA, Privacy Policy & Terms of Service apply.
Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.