Odoo 16 Development Book

Init Hooks

The manifest file's init hook can be used to perform any operation you desire. This section demonstrates how to use the init hook to generate some records.Post_ init_hook is used for this.

We are generating records using 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)

By using the key register the hook in the __manifest__.py file, this is the first step. Post_init_hook. Odoo will look for the corresponding method in the init file and execute it after the module has been installed.

'post_init_hook': 'create_student',

In the init file, add the create_student() method. In this method, we use the create method to create a sample student record.

In a real-time scenario, we can implement complex business logic here.

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

Additionally, Odoo supports two more hooks.

Those are pre_init_hook hook and uninstall_hook. The pre_init_hook will be called before the module installation. The uninstall_hook is called at the time of module installation.



Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message