How to Inherit a Super Function in Odoo

Abid Patel
06-Nov-2024 Updated : 06-Nov-2024

Learn how to inherit and use super functions in Odoo. This guide covers extending existing methods and adding custom logic with clear examples in Odoo 16.

How to Inherit a Super Function in Odoo

In Odoo inheritance is crucial concept. It allows to modify or expand the functionality of existing models or methods. When someone inherits a class or function, they may fully override it. Alternatively they can keep the original function. This can be done using super() function.

What does Super Function mean in Odoo?

In Odoo super function points to the method from the parent class. It can be an original function. This function can be invoked within an inherited class. We see this function's use when we override method. However we may also want to call the original method of parent class. This is needed to preserve the parent's class functionality.

With super() it is possible to call the parent class method. The use of this function enables child class to modify behavior. It can also allow a child to extend the behavior. But this happens without completely replacing the original method, thus maintaining parent class method use.

Example: Inheriting a Super Function in Odoo

Imagine we desire to expand the functionality of create() method in sale.order model. This method's role is in creation of new sales orders in Odoo. However we want to insert custom logic. Say, for instance we insert a note after a new order is created.

Here is the way to approach this:

Step 1: Take Over the Sale Order Model

We will take over sale.order model. We will override the create() method. However, we won't totally replace the create() method. We will use super() to call original function. It is critical to use super(). After calling the original function using super() we add custom functionality.

python

from odoo import models, fields, api

class CustomSaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.model
    def create(self, vals):
        # Call the super function to execute the original create method
        order = super(CustomSaleOrder, self).create(vals)
        
        # Add custom logic: add a note to the sale order
        order.message_post(body="This order has been created with custom logic.")

        # Return the order record created by the super function
        return order

Step 2: Explanation of Code

  • ▹ Inherit sale.order Model. We inherit sale.order model. We do this using _inherit attribute. This ensures that our CustomSaleOrder class extends existing functionality of sale.order model.
  • ▹ Override create() Method. The create() method is overridden in our custom class. We modify the default behavior when creating a new sale order.
  • ▹ Call Super Method. super(CustomSaleOrder self).create(vals) calls parent class's create() method. It passes vals, which are the values for new record. This line is crucial. It ensures that original behavior happens first. We create a new sale order. Then we add custom logic.
  • ▹ Add Custom Logic. Once the original create() method is called we add our custom logic. We post a message on sale order. The message_post() method allows you to add a note to the order. This can be displayed on the record.
  • ▹ Return Created Order. Lastly, we return the order object. This makes sure that the newly sale order is returned. It includes our custom changes and can be used as expected.

Step 3: Test the Custom Behavior

To test functionality follow steps provided:

  • ▹ Create new sale order in Odoo.
  • ▹ Once you create the order a message posts. This demonstrates that the custom logic is functioning as intended. This confirmation is important. Make a note to check for it.
  • ▹ Chatter or message history of the sale order is where we will find this message. It's the result of our message_post() method. This method is what adds notes to the orders. These notes often appear on records.

Why Use super() in Odoo?

  • Super() use is for the purpose of preserving core functionality. It lets you extend methods that are in place. This becomes especially handy when you aren't looking to totally override a method. You want to add more behavior. Use of super() is another name for avoiding redundancy.
  • ▹ Not using super means you need to rewrite the whole method. This leads to repetition. It also means complexity in maintenance is escalated. Super() usage eliminates the chance of rewriting code needlessly. Code that is already present in parent class.
  • ▹ Super() also ensures updated compatibility. Odoo is known to update its core codebase regularly. It ensures that any customizations you have done are not left behind. If Odoo does change a parent method, super() call makes sure that the custom code is correctly based. It keeps up with the new functionality.

Conclusion

Inheriting calling super function in Odoo can extend functionality. This can happen while retaining original behavior. Using super(), adding custom logic to methods is simple. This is key for tailoring Odoo to business needs without breaking core features.

In this blog, we learn to inherit create() method of the sale.order model. We add custom functionality. This pattern is useful for many other methods and models in Odoo. It is crucial for Odoo developers.

By following these steps extending Odoo's built-in methods is easy. This ensures that customizations stay compatible with future updates.

Make a Comment

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