4

I am trying to write a customize Odoo module which need to call some form of PRE-installation or POST-installation function / hook , wandering any of you have this knowledge which can share with me how to go about it ?

Sample of process : e.g. During installation , when user click to install the custom module , it will call a Pre-installation hook and do some initialization, copying of files or other and then after that once it finished , we can capture the return installation process of Odoo and run a Post-installation hook. And it will only execute once during the time where the module install / upgrade.

Do appreciate to share a pointer of where this Pre / Post installation code should go into my module code ?

Thanks Kalmen

Kalmen Chia
  • 43
  • 2
  • 5

1 Answers1

3

You just need to add a hooks.py file to your module. Then in your __openerp__.py file add the hooks you want to execute:

"post_init_hook": "post_init_hook",
"pre_init_hook": "pre_init_hook",

Then in the file write the methods to update the records you want, you can use the orm and execute queries, for example:

def pre_init_hook(cr, registry):
    cr.execute('ALTER TABLE res_partner'
               'ADD COLUMN new_column character varying;')
    cr.execute('UPDATE new_column'
               'SET new_column = phone;')
    # in the installation the column phone is dropped

def post_init_hook(cr, registry):
    partners = env['res.partner'].search([])
    for partner in partners:
        if partner.new_column:
           #do something
    cr.execute('select new_column from res_partner')
Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26
Max Power
  • 31
  • 3
  • 1
    Just adding to this - the `env` variable isn't passed to the pre/post_init_hook functions. You'll need to do something like `env = api.Environment(cr, SUPERUSER_ID, {})` to get a valid Environment (For 9.0 at least. Should be similar for 10.0/11.0) – Ryan Cole Aug 09 '17 at 02:33