Enable Dark Mode!
how-to-use-init-hooks-in-odoo-15.jpg
By: Mily Shajan

How to use Init Hooks in Odoo 15

Technical Odoo 15

In Odoo, hooks are functions that are presented as a string in the __init__ file of a module. They are the functions that can execute before and after the existing code.
They are defined inside the __manifest__ file. They are used to create some records.
Odoo hooks are in different types, such as
a) Post_init_hook
b) Pre_init_hook
c) Uninstall_hook
d) Post_load hook
In this blog, it describes the Init Hooks in Odoo 15.
a) post_init_hook
Post_init_hook are the functions that will execute after the module installation. It is added inside the __init__ 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.
from odoo import fields, models
class College(models.Model):
  _name = "college.student"
  _description = "College"
  name = fields.Char(string="Name", required=True)
  phone = fields.Char(string="Phone Number")
  email = fields.Char(string="Email", required=True)
This code creates the data for the college.student model
For the post_init_hook, 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.
It can give as follows.
'post_init_hook': 'create_colleges',
Add the method create_colleges in the __init__ file of the module. This method that executes after installing the module will help to create college records as a sample one.
from odoo import api, SUPERUSER_ID
def create_colleges(cr, registry):
  env = api.Environment(cr, SUPERUSER_ID, {})
  env['college.student'].create({
      'name': 'Demo',
      'phone': '+7865 6675 2341',
      'email': 'demo12@fmail.com'
  })
Post_init_hook has the arguments of cursors and registry. It illustrates the example of create_colleges functions that will execute after the module installation. 
The function that defines inside the __init_ file import api ,SUPERUSER_ID, fetches the env by the cursor and superuser_id and creates the colleges using create method in the college.student model. And this gives the Demo college data after the installation of the module.
Also, post_init_hook to perform some operations in the database after the installation of the module.
The next type of init hook is pre_init_hook
b) pre_init_hook
Pre-init hook that will execute the procedure of the module installation.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.
'pre_init_hook': '_pre_init_college',
The first step is to register the hook inside the __manifest__  file.
Add the _pre_init_college function is defined inside the __init__ file of the module.
def _pre_init_college(cr):
   cr.execute("""
       ALTER TABLE "college_student"
       ADD COLUMN "address_id" VARCHAR,
       ADD COLUMN "website" VARCHAR,
      
   """)
This function is added in the __init__ file which is executed before the module installation. This function creates some more fields for the college.student model.
Which adds address_id and website fields of type Character. It will create those fields before installing the module.
The pre_init_hook also adds the cursors as the parameter to the function which executes.
It helps to check the compatibility of the module. Also, it acts as a pre-preparator for each module.
So In Odoo, we can use init hooks for building the module after or before its installation. Beyond these init hooks, odoo also supports post_load and uninstall hooks.
Like these, we can add init hooks in the __init__ file of the module.


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



1
Comments

Timothy Randy

You stated that the import for init function happens before function declaration, it doesn't work for me in Odoo 12. Instead in the base module of Odoo 12, the import happens within the function declaration, not before

18/01/2024

-

4:57AM



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