Menu

Hooks for Odoo modules

In Odoo, hooks are special functions that are written as a string in a file called __init__.py within a module. These functions can run before and after the code that already exists. They are specified in the manifest file. They are used to create some records.

Odoo' hooks are in different types, such as:

  • post_init_hook
  • pre_init_hook
  • uninstall_hook
  • post_load

post_init_hook (Loaded after installing the module.)

Post_init_hook are the functions that will execute after the module installation. It is added inside the __init__.py file with the function name.

It will create some records. The arguments that can be used inside the post_init_hook are cursors and registry.

    def my_post_init_hook_function(cr, registry):
    """Loaded after installing the module.
        This module's DB modifications will be available.
        :param odoo.sql_db.Cursor cr: Database cursor.
        :param odoo.modules.registry.RegistryManager registry: Database registry.
     """
    raise NotImplementedError

we can register the hook in the __manifest__ file of the module with post_init_hook key. After the installation of the module, this method defined in the post_init_hook can be executed.

     'post_init_hook': 'my_post_init_hook_function',
 

pre_init_hook (Loaded before installing the module.)

pre_init_hook is added inside the __manifest__ file of the module, and it proceeds with a key of the function defined in the pre_init_hook. It will work when the user clicks on the 'install' button of the module. While clicking on the 'install' button, it will proceed with the function that is defined in the pre_init_hook. It works before the module installation.

Define the function my_pre_init_hook_function inside the __init__ file of the module.

    def my_pre_init_hook_function(cr):
    """Loaded before installing the module.

    None of this module's DB modifications will be available yet.

    If you plan to raise an exception to abort install, put all code inside a
    ``with cr.savepoint():`` block to avoid broken databases.

    :param odoo.sql_db.Cursor cr:
    Database cursor.
    """
    raise NotImplementedError

Then, register the hook inside the __manifest__ file.

     'pre_init_hook': 'my_pre_init_hook_function',
 
It helps to check the compatibility of the module. Also, it acts as a pre-preparator for each module.

uninstall_hook (Loaded before uninstalling the module.)

This hook will be invoked when you uninstall the module. This is mostly used when your module needs a garbage-collection mechanism.

Define the function named my_uninstall_hook_function inside the __init__ file of the module.

    def my_uninstall_hook_function(cr, registry):
    """Loaded before uninstalling the module.

    This module's DB modifications will still be available. Raise an exception
    to abort uninstallation.

    param odoo.sql_db.Cursor cr:
    Database cursor.

    :param odoo.modules.registry.RegistryManager registry:
    Database registry.
    """
    raise NotImplementedError

Then, register the hook inside the __manifest__ file.

    'uninstall_hook': 'my_uninstall_hook_function',

post_load (Loaded before any model or data has been initialized)

post_load hook is the function that execute after the module installed

If we define post_load hook inside a module, the post_load function will be executed whenever restarted

    def my_post_load_function():
    """Loaded 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.

    This is very useful to create monkey patches for odoo.

    Note: You do not have access to database cursor here.
    """
    raise NotImplementedError

To register the hook inside the __manifest__ file.

    'post_load': 'my_post_load_function',
Share this post
Developer Mode (debug mode)