This question has been flagged
21 Replies
34773 Views

In order to extend or override controller function in odoo, it's necessary to extend it as normal python class (see controllers reference ). To do so, we'll need to import the original module of controller class, but normally odoo modules are not on the sys.path... so we can't import them directly. How to import then odoo module in order to override a controller in our module?

Avatar
Discard
Best Answer

all the Odoo modules are added to the openerp.addons package, your controller A in the module testx could be imported in the module testy like:

from openerp.addons.testx.controllers.main import A
...
class my_controller(A):
@api.route()
def controller_func(self,**kw):
result = super(my_controller,self).controller_func(**kw)
       # your stuff here....
       return result

 

Avatar
Discard
Author

@Alex, your box is too small :D What about community modules? they surely are outside of openerp.addons package? As I suggest in my answer, openerp.conf.addons_paths way guarantees to include any module path (community or custom) and so, import them successfully. And if you have any custom module, it's not a recommended way to add your odoo modules to the original module folder of odoo. What about custom odoo modules, at custom path? You should ask me about, if you had some doubts buddy, I just listed 2 cases where your answer can't work... you'd better mark my answer as accepted instead of inaccurate downvote

Author

Do you know where community modules are stored as you install them in Odoo?

Odoo add every module that get loaded into the package openerp.addons, take a look at:

def initialize_sys_path(): 
""" Setup an import-hook to be able to import OpenERP addons from the different addons paths. This ensures something like ``import crm`` (or even ``import openerp.addons.crm``) works even if the addons are not in the PYTHONPATH. """
Author

It does not worked for me in case of community module. You think I've not tried simple "import module" statement before going to this workaround?

I not mean:

import module
I mean:
import openerp.addons.module

Odoo do this:

sys.modules['openerp.addons.' + module_part] = mod
for every module that need to be loaded
Author

Nice, "import openerp.addons.module" is a much simpler then a workaround I found, I'll try it. But, it's not intuitive to me, to use "openerp.addons.module" for a python module that is actually far away from addons directory.

It joins all the addons modules found in all the addons_paths in that package(that not means that odoo copy the module to the /openerp/addons folder) so a module with the same name of one in the get replaced the original one.

Author

quote: "This ensures something like ``import crm`` (or even ``import openerp.addons.crm``) " means "import module" should work as well, and "import openerp.addons.module" indicated as an additional functionality to this...

Author

And I can confirm that for "import module" that's NOT a case, you can't import community or custom module with just "import module" way (regardless of the comment quoted from odoo code), and if one does not investigates code as does @Alex, then it's not intuitive to use "import openerp.addons.module" for other modules then modules really located in odoo addons folder. I read once again my question post on this thread and I do not find anything wrong with it, all the details provided in my question post are exact and accurate, and it's downvoted, I do not get you buddies. And this question makes sense, because of existence of community modules and custom modules, outside of default addons path, what was actual problem for me (to be exact, the community one), and my answer here was perhaps ugly but complete and working solution, downvoted as well (relatively acceptable downvote compared to downvote of the question above). Your answer @Alex provide us with much more elegant way to solve the problem, so I'll mark it as accepted, I want to have marked as accepted the best answers on my questions, counting that I do not always get one. Thanks

Author

* @Axel, not Alex :)

it's a valid question, I upvote it to get it > 0

Author

Thanks @Axel :) BTW I figured out that I've some mysterious fan on this forum, it's Dr Obx, it was he, who downvoted my question above. He is very interesting person, first he downvoted question and I got -1, then he took back his downvote and the -1 turned in zero(but it leaves effect of -1 in this forum). He've also downvoted few other questions/answers and I've got 6 negative vote from him, by now his votes some are -1, some are 0 (0 means he took back his vote, either negative or positive, but on this post I've seen that it was negative), but not even one +1... Perhaps as a being human I happen to write bad Q/A sometimes, but not always I think... and there is other interesting pattern in his behavior as well, he generally downvotes already upvoted posts (for example this one), so his downvote stays invisible, downvote to this post was an exception to this pattern, but it was part of another, he takes back his downvotes, when it's no more invisible. For now, he is author of about half negative votes I ever got on this forum (in contrast of being far from authoring half of positive votes), and all of -1 are invisible according his pattern, he first show his face on this post. I simply interested if I gave him any vote, and that's a case, I upvoted +1 his post once, in addition to helping him and accompanying him down to the solution, and that's all my votes to him. For now, I understand his behavior and when and how and according which patterns he follows me, but I do not yet understand why, because all his downvotes are far from being honest :) of course I'm not going to do same to him, as he will run out of the necessary carma to post anything here, and so, I'll stay with no fans, now at least I have interesting one :)

Author
maybe he'll respond there? He may have some original idea though... who knows...

Sorry to hear that. Maybe he could bring a light to his behavior

Author

No, you have not to be, We have not to be sorry... he's just exception fortunately, "Dr Dislike", most of people are cool out there... Only thing I'm sorry about is that I spent too much time to investigate his behavior on this forum... But it was interesting anyway, as a new thing :) Next time I'll ignore such cases if any...

Author

for anyone interested, this way was posted somewhere on this forum, you can put it into the browser console:

var get_likes_by_post = function(post_id) {
    openerp.jsonRpc('/web/dataset/call_kw', 'call', {
        model: 'forum.post.vote',
        method: 'search_read',
        args: [[['post_id','=',post_id]], ['user_id', 'vote']],
        kwargs: { context: openerp.website.get_context()}
    }).then(function(result) { 
        function vote(x) { 
            this.user_name = x.user_id[1];
            this.user_id = x.user_id[0];
            this.vote = parseInt(x.vote);
        }
        res = [];
        _.forEach(result, function(x){ res.push(new vote(x)); }); 
        console.table(res, ['vote','user_id','user_name']);
    })
}
for this question post, it'll like:
get_likes_by_post(87139)

It doesn't work for me!, i can't import a custom module, i just need to put my custom module or the community module under /odoo/openerp/addons/ directory so that i can use import from openerp.addons.mymodule

Hi Mustafa, seems that your modules are not loaded correctly because you don't set correctly the addons_path option in the config or command-line

Author Best Answer

Adding those modules to sys.path just before importing it in our python code may be a solution if we know/find path to the imported module, but such code may NOT be portable in some cases. fortunately there is pretty nice solution if we consider to use configuration entry inside the odoo: openerp.conf.addons_paths, using value of this config we can find/import odoo module as follows:

from openerp import conf
import imp

fp, pathname, description = imp.find_module('odoo_module_to_import',conf.addons_paths)
module_mod = imp.load_module('odoo_module_to_import', fp, pathname, description)

that's it. in the module_mod we have imported odoo module named "odoo_module_to_import". If we imagine that odoo modules are on the sys.path and we can import them directly, then the above code should be equivalent of :

import odoo_module_to_import as module_mod

now you can use module_mod in order to inherit your own controller from it and override/extend functions in the parent controller, for example:

class my_controller(module_mod.controllers.parent_controller):
@api.route()
def controller_func(self,**kw):
result = super(my_controller,self).controller_func(**kw)
# your stuff here....
return result

Avatar
Discard

This solution worked for me

Best Answer

i create page has 3 classes and every class inherit from model

it is work for first time then show to me Key error

Avatar
Discard

I don't see anything related, could you explain a little more your issue? or post a question with your code so someone could help you to fix your issue