Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

1

How to import odoo module?

By
Temur
on 6/23/15, 8:35 AM 3,257 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?

2

Axel Mendoza

--Axel Mendoza--
10263
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 10/9/15, 12:30 PM

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

 

@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

Temur
on 10/9/15, 12:49 PM

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

Temur
on 10/9/15, 12:54 PM

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. """
Axel Mendoza
on 10/9/15, 12:58 PM

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?

Temur
on 10/9/15, 1:07 PM

I not mean:

import module
I mean:
import openerp.addons.module
Axel Mendoza
on 10/9/15, 1:13 PM

Odoo do this:

sys.modules['openerp.addons.' + module_part] = mod
for every module that need to be loaded
Axel Mendoza
on 10/9/15, 1:16 PM

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.

Temur
on 10/9/15, 1:33 PM

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.

Axel Mendoza
on 10/9/15, 1:38 PM

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...

Temur
on 10/9/15, 1:38 PM

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

Temur
on 10/9/15, 2:37 PM

* @Axel, not Alex :)

Temur
on 10/9/15, 2:42 PM

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

Axel Mendoza
on 10/9/15, 2:42 PM

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 :)

Temur
on 10/12/15, 10:18 AM
maybe he'll respond there? He may have some original idea though... who knows...
Temur
on 10/12/15, 11:13 AM

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

Axel Mendoza
on 10/12/15, 11:25 AM

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...

Temur
on 10/13/15, 3:44 AM

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)
Temur
on 10/13/15, 4:04 AM
Axel Mendoza
on 12/16/15, 5:22 PM

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

mustafa
on 10/30/16, 8:41 AM

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

Axel Mendoza
on 11/21/16, 1:40 PM
1

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 6/23/15, 8:38 AM

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

This solution worked for me

mustafa
on 10/31/16, 8:02 AM
0
omar bahri
On 6/27/16, 5:00 AM

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

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

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

Axel Mendoza
on 6/27/16, 5:11 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 6/23/15, 8:35 AM
Seen: 3257 times
Last updated: 10/30/16, 8:48 AM