Uninstall Hook in Odoo 17 - Odoo 17 Slides

CelineGeorge1 381 views 7 slides Oct 01, 2024
Slide 1
Slide 1 of 7
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7

About This Presentation

Hooks are introduced to Odoo to perform additional operations during the installation, uninstallation, upgrade, or other key events of a module. By defining and registering these hooks, we can control and extend the behavior of our module during key stages of its lifecycle.


Slide Content

Uninstall hook in Odoo 17 Enterprise

Introduction Enterprise Hooks are introduced to Odoo to perform additional operations during the installation, uninstallation, upgrade, or other key events of a module. By defining and registering these hooks, we can control and extend the behavior of our module during key stages of its lifecycle. In Odoo 17, creating an uninstall hook allows us to execute specific actions when a module is being uninstalled. This can be used to clean up the data, remove the custom configurations, and to reverse the changes that were made during the module installation.

Enterprise To use uninstall hook in Odoo 17, there are two steps we need to take care of. Define the uninstall hook function This means to create the function which have the logic that is to be executed during the uninstallation of the module. As per OCA standard, it's better to define this method in the __init__.py file of the module. Or we can keep this in any other relevant file in the module. Register the uninstall hook We can register the uninstall hook in the __manifest__.py file.

Enterprise Let’s go with an example code. Suppose we have a custom module named ‘my_module’ and we want to deactivate/archive all the menus created from that module during its uninstallation, we can set the uninstall_hook() inside __init__ file as # my_module/models/uninstall_hook.py def uninstall_hook(env): res_ids = env['ir.model.data'].search([ ('model', '=', 'ir.ui.menu'), ('module', '=', 'my_module') ]).mapped('res_id') env['ir.ui.menu'].browse(res_ids).update({'active': False}) Here, from the base ir.model.data model, res_id of all the menus created from the my_module is searched with the domain and saved it in the variable res_ids. Then, all of those menu’s active field is set as False, which means disabled.

Enterprise Next step is to register the hook in the manifest file. It’s done by adding the key uninstall_hook in the manifest. That is # my_module/__manifest__.py { 'name': 'My Custom Module to show Hooks', 'version': '1.0', 'depends': ['base'], 'data': [ # List your data files here ], 'installable': True, 'auto_install': False, 'uninstall_hook': 'uninstall_hook', } The uninstall_hook key in the __manifest__.py file points to the path of the function within the module.

Enterprise Make sure that the path is correctly given in the manifest. Also the uninstall hook function receives two parameters: cr (cursor) and registry. Use these to interact with the Odoo environment and perform your cleanup tasks.

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