Enable Dark Mode!
how-to-use-hooks-in-odoo-development.png
By: Milind Mohan P

How to Use Hooks In Odoo Development?

Technical

Hooks are the functions that execute either before, after, or instead of the existing code. In odoo hooks are the string representation of the function that is defined inside the __init__.py of a module.

In odoo hooks are of the following types,

1. pre_init_hook

2. post_init_hook

3. uninstall_hook

4. post_load

Hooks are defined inside the __manifest__.py file inside a module using the above keywords as follows.

{
    '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 strings in the value positions of the manifest proceeding the hook keys are the function names which is defined inside the __init__.py file 

According to the OCA guidelines the hook functions should be placed inside the hooks.py file which is located inside the root of the module directory. Also remember to add into __init__.py file and import as follows,

from .hooks import pre_init_hook, post_init_hook, uninstall_hook, post_load

If you follow OCA guidelines for using hooks, your module structure will be something like the below,

how-to-use-hooks-in-odoo-development

Let’s check how to write hooks and what are the uses of hooks in Odoo Development for each hook in detail.

Pre Init Hook

Pre Init Hooks are the hook functions that execute prior to the module installation. If we define a pre_init_hook inside a module, the hook function will be executed whenever a user clicks the install button of that module. The pre_init_hook function will be invoked and perform some operations prior to the module installation.

The pre_init_hook takes the cursor as its only argument, for example, you can define a pre_init_function as follows.

You can define a pre_init_function as follows.

First, you need to write a manifest file as follows,

how-to-use-hooks-in-odoo-development

Then you have to write the function definition inside hooks.py file inside your module and in __init__.py as follows

how-to-use-hooks-in-odoo-development

Inside the hooks.py file define pre_init_hook function with name provided in the manifest,

how-to-use-hooks-in-odoo-development

You can use this hook before registering the module's logic in their.module.module. Pre_init_hook’s are commonly used for checking the compatibility of the module. By using pre_init_hook we can perform some modifications in the database for preparing it for the module installation.

Post Init Hook

These are the functions that execute right after the module installation. The arguments of post_init_hook functions are database cursor and registry.

You can define a post_init_function as follows.

In your manifest file

how-to-use-hooks-in-odoo-development

Import the post_init_hook function in your __init__.py file and define the function as follows.

how-to-use-hooks-in-odoo-development

Inside the  hooks.py file define the function as follows,

how-to-use-hooks-in-odoo-development

You can use the post_init_function to perform some operations inside the database for finalizing the module installation.

Uninstall Hook

This function is executed right after the uninstallation of the module, the arguments of the uninstall hooks are the database cursor and the registry.

You can define uninstall_hook function as follows.

In your manifest file.

how-to-use-hooks-in-odoo-development

Import the uninstall_hook function in you __init__.py file and define the function inside the hooks.py file as follows.

how-to-use-hooks-in-odoo-development

Inside hooks.py file define the uninstall hook function as follows,

how-to-use-hooks-in-odoo-development

You can use the uninstall_hook function for completing the uninstallation process of the module or to clean up some records in case it's impossible through API.

Post Load

According to odoo commends, post_load are for,

“Call the module's post-load hook. This can be done before any model or #data has been initialized. This is ok as the post-load hook is for #server-wide (instead of registry-specific) functionalities.”

Normally these functions are used for Monkey Patches.

Monkey Patches: Monkey patching refers to the dynamic or runtime modification of a class method, we can change the behavior of a function or a code in runtime to get the desired output.

Monkey Patching eg:

how-to-use-hooks-in-odoo-development

The reason behind using the post_load for monkey patching is that otherwise the monkey patches will be applied every time when it's available in the path. By using post_load it will be applied once only if the corresponding module is installed.

You can define the post_load as follows,

Add the post_load function name inside the module’s manifest file,

how-to-use-hooks-in-odoo-development

Import the post_load function in your __init__.py file and define the function inside hooks.py file as follows.

how-to-use-hooks-in-odoo-development

how-to-use-hooks-in-odoo-development

You can use post_load as above, post_load will be applied just if the module is installed.

According to the odoo documentation, we can use hooks when setup or cleanup is required while installing or uninstalling a module and also they suggest that hooks should only be used when some operations need to be done while the installation or uninstallation process required for this module and is either extremely difficult or impossible through the API.

You also check our video How to Use Post Init Hook in Odoo 15


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message