Back to Top

Exploring External Dependencies ,Pre and Post INIT Hook In Odoo

Updated 1 October 2019

In the __opererp__.py you can use the special keys like:

  • external_dependencies
  • pre_init_hook
  • post_init_hook

These keys can be very useful for doing lots of stuff like validating the module compatibility and it’s required dependency ,running some script at the time of installation .

External Dependencies

In the __openerp __.py you can use external_dependencies key for   declaring   all the external dependency required for the module.
For Example :
As one of my stripe payment module required a python dependency of stripe library so we have use external_dependencies as:
'external_dependencies': {
        'python' : ['stripe'],
    },

At the time of installation if system does not have stripe library installed , it will raise an error and not allow to install the module.

pre init hook and POST INIT HOOK

The ‘pre_init_hook‘ is also a useful key available in __openerp __.py .

For running the logic before registered the module in the ir.module.module you can use ‘pre_init_hook’.

Searching for an experienced
Odoo Company ?
Find out More

For Example :

As many times we also have validate that the module build for Odoo8 should not be installed in Odoo9 in that case we use pre_init_hook .

For using the pre_init_hook define this key in your __openerp__.py and assing it’s a method created in your __init_.py of module.
In  __opnerp__.py:

'pre_init_hook':'pre_init_check',

In  __init__.py:

 

def pre_init_check(cr):
	# from openerp.service import common
        # from openerp.exceptions import Warning
	# version_info = common.exp_version()
	# server_serie =version_info.get('server_serie')
	# if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie))
	return True


We can define the ‘post_init_hook‘ in the same ways:
In  __openerp__.py:

'post_init_hook': 'post_init_check',

In  __init__.py:

def post_init_check(cr, registry):   
    # from openerp.service import common
    # from openerp.exceptions import Warning
    # version_info = common.exp_version()
    # server_serie =version_info.get('server_serie')
    # if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie))
    return True

 

SAMPLE Code

Finally  __openerp__.py look like:

....
    'external_dependencies': {
        'python' : ['stripe'],
    },
    'pre_init_hook':'pre_init_check',
    'post_init_hook': 'post_init_check',   

....

And __init__.py look like:

# -*- coding: utf-8 -*-
import models
import controllers
def post_init_check(cr, registry):   
    # from openerp.service import common
    # from openerp.exceptions import Warning
    # version_info = common.exp_version()
    # server_serie =version_info.get('server_serie')
    # if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie))
    return True
def pre_init_check(cr):
	from openerp.service import common
	from openerp.exceptions import Warning
	version_info = common.exp_version()
	server_serie =version_info.get('server_serie')
	if server_serie!='8.0':raise Warning('Module support Odoo series 8.0 found {}'.format(server_serie))
	return True


For further code reference you can visit this links over GitHub .

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home

Table of Content