How to Use Init Hooks in Odoo 16 ERP

How to Use Init Hooks in Odoo 16 ERP

4 minutes, 39 seconds Read

Hooks are functions that run before, after, or in place of existing code. Hooks, functions that are displayed as strings, are contained in the __init__ .py file of an Odoo module.
Hooks are defined within a module’s __manifest .py file using the keywords listed below.
The types of hooks used in Odoo include
1. pre_init_hook
2. post_init_hook
3. uninstall_hook
4. post_load
1. post_init_hook
Post init hook functions are those that run after the module is installed. It is added with the function name inside the __init__.py file. It will generate some data. Cursors and registry are arguments that can be passed to the post init hook.
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)
We can use the post_init_hook key to register the hook in the module’s __manifest__.py file.
After the module has been installed, the method defined in the post_init_hook can be called.
You can define a post_init_function in your manifest file, as shown below.
'post_init_hook': 'create_employees',
In your __init__.py file, import the post_init_hook function and define it as shown below.
from odoo import api, SUPERUSER_ID
def create_employees(cr, registry):
  env = api.Environment(cr, SUPERUSER_ID, {})
  env['company.employee'].create({
      'name': ABC,
      'phone': '+7865 6675 2341',
      'email': abc123@gmail.com'
  })
In the module’s __init__.py file, add the method create_employee. This procedure, which is executed after the module has been installed, will support producing employee records, as an example.
Cursors and registry are passed as arguments to post_init_hook. It shows an example of the create_employee functions that will be executed after the module is installed.
The function is defined within the __init_.py file import api, SUPERUSER ID, retrieves the environment using the cursor and superuser_id and creates the employees using the create method in the company. employee model.
After installing the module, this returns the example data of the employee.
For the purpose of concluding the module installation, you can use the post_init_function to carry out a few database operations.
2. pre_init_hook
Pre Init Hooks are the hook operations that run before the installation of the module.
When a user clicks the module’s install button, the pre_init_hook function that was defined inside the module will be called. Prior to the module installation, the pre_init_hook function will be used to carry out a few operations.
We can check the example for pre_init_hook with the above-created model company.employee.
You can define a pre_init_hook function in your manifest file, as shown below.
'pre_init_hook': '_pre_init_employee',
In the module’s __init__ .py file, the _pre_init_employee function is defined.
The cursors are also added as a parameter to the function that is executed by the pre_init_hook.
def _pre_init_employee(cr):
   cr.execute("""
       ALTER TABLE "company_employee"
       ADD COLUMN "place" VARCHAR,
       ADD COLUMN "job" VARCHAR,   
   """)
In order to prepare the database for the installation of the module, we can make some changes using pre_init_hook.
This function is now included in the __init__ file, which is executed prior to module installation. For the company.employee model, this function adds a few new fields.
3. Uninstall_hook
This function is called immediately after the module is uninstalled; the uninstall hooks’ arguments are the database cursor and the registry.
You can define a Uninstall_hook function in your manifest file, as shown below.
'uninstall_hook': 'uninstall_hook'
In the __init__ .py file should import the uninstall hook function and define the function inside the hooks.py file as follows.
from .hooks import uninstall_hook
The uninstall hook function is defined in the hooks.py file as follows:
def uninstall_hook(cr, registry):
""" Uninstall hook functions"""
env = api.Environment(cr, SUPERUSER_ID, {})
     # do things
4. Post Load
Before any model or #data has been initialized, this can be done.This is fine because the post-load hook is used for server-wide (rather than registry-specific) functionality.”
Typically, Monkey Patches use these functions.
Monkey Patches
Monkey patching is the term for the dynamic or runtime alteration of a class method. To change the behavior of a function or a piece of code at runtime and get the desired result, see monkey patching.
For example, monkey patching:
class Test:
   	 def method class(self):
        	print("Class method)
       	 
def monkey_patch():
   	 print("Monkey Function")
   	 
	 Test.method_class = monkey_patch
	 Test.method_class()
The reason for using post_load for monkey patching is that otherwise the monkey patches will be applied whenever they are found in the path. If the corresponding module is installed, post_load will be used only once.
You can define a post_load function in your manifest file, as shown below.
'post_load': 'post_load'
In the __init__ .py file should import the post load function and define the function inside the hooks.py file as follows.
from .hooks import post_load
The post load function is defined in the hooks.py file as follows:
def post_load():
""" post_load functions"""
# monkey patch
This includes the addition of place and job fields of type Character. Those fields will be created prior to installing the module. It is useful to examine the module’s compatibility. It serves as a pre-preparator for each module as well. In order to build a module in Odoo, either after or before installation, we can use init hooks.

Similar Posts

Leave a Reply

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