Contributors mailing list archives
Re: How to write hook addon properly.by Richard deMeester <email@example.com> - 11/03/2019 22:48:36
- my solution will always cause a reassignment of the base method to the new method;
- the _register_hook solution will cause the reassignment only if the module is installed.
# do the patching, and detect if it happend beforeI guess this is just a proper fail-safe, because it if is a simple assignment, then it shouldn't matter if it has happened before. Or does it?
Senior Development Analyst
WilldooIT Pty Ltd
M: +61 403 76 76 76
P: +61 3 9135 1900
A: 10/435 Williamstown Road, Port Melbourne, Vic 3207
Our vision is to provide end to end IT solutions to help our clients achieve their desired objectives and provide growth opportunities for everyone working in the company to reach their full professional potential.
DISCLAIMER | This electronic message together with any attachments is confidential. If you are not the recipient, do not copy, disclose, or use the contents in any way. Please also advise us by e-mail that you have received this message in error and then please destroy this email and any of its attachments. WilldooIT Pty. Ltd. is not responsible for any changes made to this message and/or any attachments after sending by WilldooIT Pty. Ltd. WilldooIT Pty. Ltd. use virus scanning software but exclude all liability for virus or anything similar in this email or attachment.
Sent: Friday, 8 March 2019 5:31 PM
Subject: Re: How to write hook addon properly.
some nitpickings > All of the python code is "loaded and available" and the classes are > instantiated Odoo loads (as in `import ...`) all code that's in the addon path. It does not instantiate the classes defined in non installed modules as in `my_class()` > whether the module is installed or not - Odoo will control, > by knowledge of the modules installed, which methods are to be called and > in which order. actually, the framework won't control anything for non-installed modules. And it can't and it shouldn't. If a module needs monkey patching I'd suggest to do this in _register_hook of some model, then you know your module is actually installed. For models, Odoo will create python classes on the fly mimicking the inheritance chain of the modules installed, so if you have modules A and B both having a model inheriting from res.partner, and A depends on B, Odoo will carve classes that in python inheritance look somewhat like base#res.partner B#res.partner (inherits from base#res.partner) A#res.partner (inherits from B#res.partner) If you need to do fancy stuff with inheritance, inspect self.__mro__ - this is more or less where python stores the chain of functions to call when you say super(...). You can also manipulate this programmatically, or call just the version of the function you need. So what the example code should be doing is class MrpProductProduce(models.TransientModel): _inherit = "mrp.product.produce" def _register_hook(self): # do the patching, and detect if it happend before # (models are registered with every registry reload, that can happen # on run time too) # cf https://github.com/OCA/OCB/blob/9.0/openerp/models.py#L5329 return super()._register_hook() #