What is the Best Way to Use Hooks in Odoo Development?

November 3, 2023

The routines known as hooks run either before, after, or in place of the current code. Hooks in Odoo are the string representation of a function declared in a module’s __init__.py file.

The following types of hooks are found in Odoo:


2. Initiate a post

3. remove_hook

4. after_load

Using the aforementioned keywords, hooks are defined as follows inside the __manifest__.py file inside a module.


    'pre_init_hook': 'test_pre_init_hook',

    'post_init_hook': 'test_post_init_hook',

    'uninstall_hook': 'test_uninstall_hook',

    'post_load': 'test_post_load',


The function names that are defined in the __init__.py file are the strings that appear in the manifest’s value locations before the hook keys.

The hook routines should be put inside the hooks.py file, which is found inside the module directory’s root, in accordance with OCA requirements. Don’t forget to import and add to the __init__.py file as well.

from .hooks import pre_init_hook, post_init_hook, uninstall_hook, post_load

If you use hooks in accordance with OCA rules, your module structure will be like the one below.


Let’s examine the writing of hooks and the specific applications for each hook in Odoo Development.

Pre Init Hook

The hook routines known as pre-init hooks run before the module is installed. When a user clicks the install button on a module, the hook function will be called if the pre_init_hook is defined inside the module. A few tasks will be completed by the pre_init_hook function before the module installation.

The cursor is the only argument that the pre_init_hook accepts. For instance, you can define a pre_init_function like this.

The following is how to define a pre_init_function.

Writing a manifest file requires you to do the following first:


The function definition must then be written as follows in the __init__.py and hooks.py files inside your module.

The pre_init_hook function is defined in the hooks.py file and has the name given in the manifest.

Prior to registering the logic of the module in their. module. module, you can utilize this hook. Pre_init_hooks are frequently used to verify that a module is compatible. We can make some changes to the database to get it ready for the module installation by using pre_init_hook.

Post Init Hook

These are the processes that start running as soon as the module is installed. Database cursor and registry are the arguments of the post_init_hook methods.

The following is how to define a post_init_function.

The manifest file in your


In your __init__.py file, import the post_init_hook function and declare it as such.


The function is defined in the hooks.py file as follows:

The module installation can be completed by using the post_init_function to carry out a few database activities.

Uninstall Hook

The registry and database cursor are the arguments of the uninstall hooks, which are called immediately after the module has been uninstalled.

The uninstall_hook function can be defined as follows. the manifest file you have.


The uninstall hook function is defined in the hooks.py file as follows: In your manifest file.


The use of hooks in Odoo development

If using the API isn’t feasible, you can utilize the uninstall_hook function to finish the module’s uninstallation or to clear out some records.

Post Load

Odoo commends state that post_load is for

“Handle the post-load hook for the module. This can be completed prior to the initialization of any models or #data. This is acceptable since the post-load hook is meant for server-wide features rather than registry-specific ones.

These features are typically applied to monkey patches.

The term “monkey patching” describes the dynamic or runtime alteration of a class method. In order to obtain the intended result, we can alter the behavior of a function or piece of code at runtime.

Monkey Patching, such as:


For monkey patching, the post_load is used because otherwise, the monkey patches will be applied each time they are found in the route. Post_load will only be used once in the event that the relevant module is installed.

The post_load can be defined as follows:

Include the name of the post_load function in the manifest file for the module.


In your __init__.py file, import the post_load function. Then, define the function as follows in the hooks.py file.


If the module is installed, post_load will only be used as described above.

The Odoo documentation states that we can use hooks for setup or cleanup during the installation or uninstallation of a module. However, they also recommend that hooks be used only in those situations where certain operations are required during the installation or uninstallation of a module and cannot be completed through the API or are very difficult.

"Unlock the Full Potential of Your Business with Odoo ERP!"

"Get a Cost Estimate for Your ERP Project, Absolutely FREE!"

Get a Free Quote

Leave a Reply

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