Enable Dark Mode!
what-are-the-different-types-of-hooks-in-odoo-17.jpg
By: Cybrosys Technologies

What Are the Different Types of Hooks in Odoo 17

Technical Odoo 17

Hooks are mechanisms that run before, following, or in lieu of existing code. These functions, presented as strings, are stored in the init.py file of an Odoo module.

 The module's manifest.py file delineates these hooks using specific keywords, such as 

1. Pre_init_hook 

2. Post_init_hook

3. Uninstall_hook

1. post_init_hook

Post init hook functions are executed after the module is installed. These functions are included by specifying their names within the init.py file and are typically responsible for generating certain data. The post init hook can receive argument as environment(env).

For example,

The below code generates the data for the company.employee model.

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

The post_init_hook key is employed to enlist the hook in the manifest.py file of the module. Once the module is installed, the method specified in the post_init_hook is invoked. 

In the manifest file, you can define a post_init_function as demonstrated below.

'post_init_hook': 'create_employees',

In the init.py file, import the post_init_hook function and declare it as illustrated below.

def create_employees(env):
    env['company.employee'].create({
      'name': ABC,
      'phone': '+7865 6675 2341',
      'email': abc123@gmail.com'
  })

In the init.py file of the module, incorporate the create_employee method. This procedure, executed post-installation, facilitates the generation of employee records as an illustrative case. The post_init_hook receives the environment as an argument, showcasing an instance of the create_employee function that executes subsequent to module installation. Within the init.py file, 

To conclude module installation, the post_init_function can be employed for executing specific database operations.

2. Pre_init_hook

Pre Init Hooks are operations that are executed before the module is installed. When a user initiates the installation process by clicking the module's install button, the pre_init_hook function specified within the module is invoked. Before the actual installation of the module, the pre_init_hook function is utilized to perform certain operations. An example of pre_init_hook with the previously created model company.employee is demonstrated. 

You can declare a pre_init_hook function in your manifest file, as indicated below.

'pre_init_hook': '_pre_init_employee',

Within the init.py file of the module, the _pre_init_employee function is established. The function executed by the pre_init_hook is also augmented with the inclusion of environment as a parameter.

def _pre_init_employee(env):
   env.cr.execute("""
       ALTER TABLE "company_employee"
       ADD COLUMN "place" VARCHAR,
       ADD COLUMN "job" VARCHAR,   
   """)

To set the database for module installation, adjustments can be made utilizing pre_init_hook. This function is currently incorporated in the init file and runs before the module is installed. Specifically for the company.employee model, this function introduces several additional fields.

3. Uninstall_hook

This function is invoked promptly after the module is uninstalled, with the uninstall hook receiving the database environment as an argument.

You can declare an Uninstall_hook function in your manifest file, illustrated as follows.

'uninstall_hook': 'uninstall_hook'

The uninstall hook function is defined in the hooks.py file as follows:

def uninstall_hook(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()

In the above function, it deletes `account.payment.method` records created for the installed payment providers.

To read more about Hooks in Odoo 16 OWL Framework, refer to our blog What is Hooks in Odoo 16 OWL Framework


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
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