Implementing init hooks
In Chapter 6, Managing Module Data, you saw how to add, update, and delete records from XML or CSV files. Sometimes, however, the business case is complex, and it can't be solved using data files. In such cases, you can use the init hook from the manifest file to perform the operations you want.
Getting ready
We will use the same my_library module from the previous recipe. For simplicity, in this recipe, we will just create some book records through post_init_hook.
How to do it...
In order to add post_init_hook, follow these steps:
- Register the hook in the
__manifest__.pyfile with thepost_init_hookkey:... 'post_init_hook': 'add_book_hook', ...
- Add the
add_book_hook()method in the__init__.pyfile:from odoo import api, fields, SUPERUSER_ID def add_book_hook(cr, registry): Â Â Â Â env = api.Environment(cr, SUPERUSER_ID, {}) Â Â Â Â book_data1 = {'name': 'Book 1...