Hooks in Odoo serve as a powerful mechanism to execute custom operations at specific stages of a module's lifecycle. These functions are registered within the __init__.py and __manifest__.py files of an Odoo module and allow developers to modify or extend functionality before, during, or after module installation or uninstallation. In this blog, we'll explore the three main types of hooks available in Odoo 18: Pre-Init Hooks, Post-Init Hooks, and Uninstall Hooks, along with practical examples to illustrate their usage.
1. Post-Init Hooks
Post-init hooks are executed immediately after a module is installed. These hooks are particularly useful for initializing data, configuring settings, or running database operations that depend on the successful installation of the module.
To define a post-init hook, you need to:
* Declare the hook in the __manifest__.py file under the post_init_hook key.
* Implement the hook as a function in the __init__.py file of your module.
Here’s an example of creating employee records using a post-init hook:
# __manifest__.py
'post_init_hook': 'create_employees',
# __init__.py
def create_employees(env):
env['company.employee'].create({
'name': "John Doe",
'phone': '+1234567890',
'email': 'johndoe@example.com'
})
In this example, the create_employees function runs after the module installation, creating a record in the company.employee model.
Key Features of Post-Init Hooks
* They receive the environment (env) as an argument.
* Ideal for initializing data after the database schema is updated.
2. Pre-Init Hooks
Pre-init hooks are executed before the module installation begins. These hooks are typically used for preparing the database or making structural changes, such as adding or modifying fields in existing models.
To define a pre-init hook:
* Add the hook in the __manifest__.py file using the pre_init_hook key.
* Implement the hook in the __init__.py file.
Example:
# __manifest__.py
'pre_init_hook': '_prepare_employee_table',
# __init__.py
def _prepare_employee_table(env):
env.cr.execute("""
ALTER TABLE company_employee
ADD COLUMN department VARCHAR,
ADD COLUMN hire_date DATE;
""")
Key Features of Pre-Init Hooks
* Useful for modifying the database schema before installing a module.
* Helps ensure the database is ready for module installation.
3. Uninstall Hooks
Uninstall hooks are executed after a module is uninstalled. These hooks allow developers to clean up data or perform operations to ensure the system remains consistent post-uninstallation.
To define an uninstall hook:
* Specify the hook in the __manifest__.py file using the uninstall_hook key.
* Implement the hook in the hooks.py file (or any other designated file).
Example:
# __manifest__.py
'uninstall_hook': 'clean_up_payment_methods',
# hooks.py
def clean_up_payment_methods(env):
installed_providers = env['payment.provider'].search([('module_id.state', '=',
'installed')])
env['account.payment.method'].search([
('code', 'in', installed_providers.mapped('code')),
('payment_type', '=', 'inbound'),
]).unlink()
This hook ensures that payment methods associated with a module are deleted upon its uninstallation.
Key Features of Uninstall Hooks
* Executes after module uninstallation to remove related data or records.
* Helps maintain database integrity.
Hooks in Odoo provide developers with the flexibility to customize module behavior at various stages of its lifecycle. Whether you're initializing data post-installation, preparing the database pre-installation, or cleaning up data during uninstallation, understanding and utilizing hooks effectively can significantly enhance your Odoo modules. Odoo 18, with its improved features, ensures that these hooks remain a robust tool for developers.
Integrate these hooks into your modules to optimize functionality and streamline processes across your Odoo implementation.
To read more about What Are the Different Types of Hooks in Odoo 17, refer to our blog What Are the Different Types of Hooks in Odoo 17.