How to use Init Hooks in Odoo 18 - Odoo Slides

CelineGeorge1 1,006 views 11 slides Mar 05, 2025
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

In this slide, we’ll discuss on how to use Init Hooks in Odoo 18. In Odoo, Init Hooks are essential functions specified as strings in the __init__ file of a module.


Slide Content

How to use Init Hooks in Odoo 18 Enterprise

Enterprise Introduction In this slide, we’ll discuss on how to use Init Hooks in Odoo 18. In Odoo, Init Hooks are essential functions specified as strings in the __init__ file of a module. These functions execute before and after the existing code, allowing developers to customize module behavior during initialization. Init Hooks are defined within the __manifest__ file and are commonly utilized for creating records.

Enterprise In Odoo 18, several types of Init Hooks exist, each serving a specific purpose. pre_init_hook post_init_hook uninstall_hoo k post_load hook 1) pre_init_hook In Odoo, pre_init_hook is one of the init hooks, a mechanism that allows developers to execute specific functions before initialization of a module. The pre_init_hook is particularly useful for performing actions or configurations just before a module is initialized.

Enterprise In the __init__.py file, we define a function named pre_init_hook. In Odoo, pre_init_hook is one of the initialization hooks, allowing developers to execute specific functions just before the initialization of a module. Here is an example of defining and using pre_init_hook in an Odoo Module. In the init.py we will define the function. from . import models def _pre_init_sale (env) : sale_management_installed = env[ 'ir.module.module' ].search([ ( 'name' , '=' , 'sale_management' ), ( 'state' , '=' , 'installed' ) ]) if not sale_management_installed: raise ValidationError( "The 'sale_management' module is required for installation." )

Enterprise The above code defines a function _pre_init_sale that acts as a pre_init_hook in Odoo. This function checks if the 'sale_management' module is installed before the initialization of another module, otherwise raises a validation error. We can specify the pre_init_hook to instruct Odoo to execute a defined function before the module is fully initialized, during the installation process in the manifest file. { 'name' : "Company Employee" , 'version' : "18.0.1.0.0" , 'data' : [ 'security/ir.model.access.csv' , 'views/company_empolyee.xml' , ], 'pre_init_hook' : '_pre_init_sale' , 'installable' : True , 'auto_install' : False , 'application' : False , }

Enterprise 2) post_init_hook The post_init_hook is a type of init hook in Odoo that is executed after a module is initialized. It allows you to perform specific actions or configurations once the module is installed. Lets see an example, where the post_init_hook will be used to create an employee record after the module is initialized. I n the __init__.py file, you define a function named create_employees. Inside this function, we use the Odoo environment (env) to create a sample employee record with predefined values. from . import models def create_employees (env) : env[ 'company.employee' ].create({ 'name' : 'ABC' , 'phone' : '+7865 6675 2341' , 'email' : '[email protected]' })

Enterprise This function creates a sample employee record using the Odoo environment. In the __manifest__.py file, we can specify the post_init_hook to instruct Odoo to execute the defined function after the module is installed. { 'name' : "Company Employee" , 'version' : "18.0.1.0.0" , 'data' : [ 'security/ir.model.access.csv' , 'views/company_empolyee.xml' , ], 'post_init_hook' : 'create_employees' , 'installable' : True , 'auto_install' : False , 'application' : False , } As a result, after installing the module, we can find a pre-existing employee record in the 'company.employee' model.

Enterprise 3) uninstall_hook In Odoo, the uninstall_hook is another type of hook that allows developers to execute specific functions just before a module is uninstalled. This hook is useful for performing cleanup actions or handling special scenarios before a module is removed from the system. Here's an example of defining and using the uninstall_hook in an Odoo module. We will define the function in the init file. from . import models def _uninstall_hook_sale (env) : env[ 'ir.model.data' ].sudo().search([ ( 'module' , '=' , 'company_employee' ), ]).unlink()

Enterprise This function performs cleanup actions, such as deleting specific records associated with the module, before the uninstallation process is completed. We can include the uninstall_hook in the manifest file. { 'name' : "Company Employee" , 'version' : "17.0.1.0.0" , 'data' : [ 'security/ir.model.access.csv' , 'views/company_empolyee.xml' , ], 'uninstall_hook' : '_uninstall_hook_sale' , 'installable' : True , 'auto_install' : False , 'application' : False , }

Enterprise 4) post_load hook This action can be performed before the initialization of any model or data, which is acceptable since the post-load hook is designed for server-wide functionality rather than being specific to a particular registry. Monkey patches often leverage these functions, allowing for broader and server-wide modifications rather than registry-specific changes.

For More Info. Check our company website for related blogs and Odoo book. Check our YouTube channel for functional and technical videos in Odoo. Enterprise www.cybrosys.com