Init Hooks

Hooks in Odoo

Hooks in Odoo are special functions that execute before, after, or in place of the standard execution flow. They are typically used to perform custom logic during various stages of a module's lifecycle, such as installation, uninstallation, or data updates. These hook functions are defined in the module’s __manifest__.py file using specific keywords and are implemented in the module’s __init__.py file as string references to the function paths.

The types of hooks used in Odoo include

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

1. Post_init_hook

Post-init hook functions are executed immediately after a module is successfully installed. These functions are typically used to initialize or generate data required for the module’s proper operation. The hook is declared in the __manifest__.py file using the post_init_hook key, and the corresponding function must be defined and imported in the module’s __init__.py file. The function accepts two parameters—cr (cursor) and registry—which provide direct access to the database and Odoo’s model registry during execution.

For example,

The below code generates the data for the student.student model.

from odoo import fields, models
class Student(models.Model):
    _name = "student.student"
    _description = "Student"
    
    name = fields.Char(string="Name", required=True)
    phone = fields.Char(string="Phone Number")
    email = fields.Char(string="Email", required=True)

We can use the post_init_hook key to register the hook in the module's __manifest__.py file.

'post_init_hook':'create_student',

The method specified in the post_init_hook will be automatically executed after the module has been installed. To use this hook, you must first import the corresponding function in your module’s __init__.py file and then define it as shown below.

from odoo import api, SUPERUSER_ID
def create_student(cr, registry):
   env = api.Environment(cr, SUPERUSER_ID, {})
   env['student.student'].create({
       'name': 'Student 1',
       'phone': '+7865 6675 2341',
       'email': 'student1@student.com'
   })

2. Pre_init_hook

Pre-init hooks are functions that execute before the installation of a module begins. These are typically used to perform setup tasks, validations, or preliminary data handling before the module is fully installed. To implement this, you can define a pre_init_hook function and reference it in your module’s __manifest__.py file, as shown below.

'pre_init_hook': '_pre_init_student',

In the module's __init__ .py file, the _pre_init_student function should be defined.

3. Uninstall_hook

The uninstall hook function is executed immediately after a module is uninstalled. It is typically used to clean up any remaining data or perform custom actions post-uninstallation. To implement this, you can define an uninstall_hook function and reference it in your module’s __manifest__.py file, as shown below.

'uninstall_hook':'uninstall_hook'

4.Post Load

The post-load hook function is executed before any models or data are initialized, making it ideal for performing low-level operations or patching the environment early in the loading process. To use this hook, you can define a post_load function and reference it in your module’s __manifest__.py file, as shown below.

'post_load':'post_load'
whatsapp_icon
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