blog

News & Events

Working with Odoo 15 Init Hooks

Working with Odoo, there are different modules for different functionality. Each module contains code that is required to implement the functionality it is meant for. There may be a requirement to run certain instructions in certain situations for the smooth installation, running, or uninstallation of the module and hence the functionality. In such situations, a programmer can use the concept of hooks in Odoo.

Functions capable of executing before and after the existing code are called hooks in Odoo. Define these functions within the init file of the module or in a separate hooks file of its own. These hooks are specified in the manifest file so that these are considered when the code in the module is executed.

Based on when the hook executes, there are four types of hooks:

  1. post_init_hook
  2. pre_init_hook
  3. uninstall_hook
  4. post_load_hook

post_init_hook

Functions that are meant to be executed after the installation of the module are defined as the post_init_hook function. Define the function and after defining the functions set them as a post_init_hook function by mentioning the function name as follows within the manifest file, suppose the function name is create_leave_types, then in the manifest specify:

‘post_init_hook’: ‘create_leave_types’,

post_init_hooks are mostly used when there are certain database operations to be performed after the installation of a module.

pre_init_hook

Functions that are meant to be executed before the installation of the module are defined as the pre_init_hook function. Define the function and after defining the. functions set them as a pre_init_hook function by mentioning the function name as follows within the manifest file, suppose the function name is remove_leave_types, then in the manifest specify:

‘pre_init_hook’: ‘remove_leave_types’,

The pre_init_hook functions execute when the user clicks the install button of the module from the Odoo apps screen.

uninstall_hook

Functions that are executed after the uninstallation of the module are defined as the uninstall_hook function. Define the function and after defining the functions set them as a uninstall hook function by mentioning the function name as follows within the manifest file, suppose the function name is remove_leave_types, then in the manifest specify:

‘uninstall_hook’: ‘remove_leave_types’,

The uninstall hook can remove records that remain in the database even after the uninstallation of the module.

post_load

Working with Odoo, post-load hook is used for executing certain instructions after the data loaded in certain models. The postlude hook is used mostly for creating monkey-patched functions. Monkey patching of a function means changing the code of a function during runtime. Monkey patch is used as a postlude hook so that it is called only when data from the respective model is loaded.

working-with-odoo-post-load

hooks-py

test-post-lead

Request Your Free Quote

Leave a Reply

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

Open chat
1
Hello, can we help you?