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,
Hooks are defined inside the __manifest__.py file inside a module using the above keywords as follows.
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 below,
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 manifest file as follows,
Then you have to write the function definition inside hooks.py file inside your module and in __init__.py as follows
Inside the hooks.py file define pre_init_hook function with name provided in the manifest,
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
Import the post_init_hook function in your __init__.py file and define the function as follows.
Inside the hooks.py file define the function as follows,
You can use the post_init_function to perform some operations inside the database for finalizing the module installation.
This function is executed right after the uninstallation of the module, the arguments of the uninstall hooks are database cursor and the registry.
You can define uninstall_hook function as follows.
In your manifest file.
Import the uninstall_hook function in you __init__.py file and define the function inside the hooks.py file as follows.
Inside hooks.py file define the uninstall hook function as follows,
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.
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 behaviour of a function or a code in runtime to get the desired output.
Monkey Patching eg:
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,
Import the post_load function in your __init__.py file and define the function inside hooks.py file as follows.
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 needs to be done while the installation or uninstallation process required for this module and is either extremely difficult or impossible through the API.