Skip to Content
Menu
This question has been flagged
1 Reply
7071 Views

Hello !

I have to access to the ORM of openERP 7 from a python script.

I tried:

>>> import openerp

>>> pool = openerp.pooler.get_pool('test')

No handlers could be found for logger "openerp.modules.module"

>>> my_obj = pool.get('my_obj')

>>> type(my_obj)

<type 'NoneType'>

As you can see, I can't access to custom objects (e.g. tables) from outside. I've listed all the objects in the pool via pool.obj_list(), and it seems that the list is too short, even including official modules that are installed.

So, how can I access to all the objects that represents the tables of my database 'test' ?

Thanks !

PS: I can do stuff via XMLRPC instead, but I would like to use the ORM, because it's faster (already tested it).

EDIT: No one ?

Here's a more detailed example :

import openerp
import sys
sys.path.append("/home/openerp/custom_addons")
import synconfig

DBNAME = "test"
uid = 1

cr = openerp.sql_db.db_connect(DBNAME).cursor()
pool = openerp.pooler.get_pool(DBNAME)

ir_model = pool['ir.model']
data = ir_model.search(cr, uid, [])
print 'data from ir.model: %d' % len(data)

my_object = pool['my.object']
data = my_object.search(cr, uid, [])
print 'data from my.object: %d' % len(data)

Output :

data from ir.model: 334
Traceback (most recent call last):
  File "./orm_outside.py", line 20, in <module>
    my_object = pool['my.object']
  File "/home/openerp/server/openerp/modules/registry.py", line 102, in __getitem__
    return self.models[model_name]
KeyError: 'my.object'

Avatar
Discard
Best Answer

AFAIK, I don't think you can use ORM outside OpenERP without having to recreate the whole framework first.

What are you trying to achieve by accessing the ORM outside the framework?  Can it be performed within the framework instead?

Avatar
Discard
Author

Hi ! I have a socket server that will receive asynchronous responses from a previous request made inside the ORM. I can handle the asynchronous response with XMLRPC, but the ORM beeing faster, that's why I would like to use it. And also, I'm curious to know why I can access the basic modules, and not the others. Oh, and the request being asynchronous, it cannot, of course, be handled inside the ORM.

There are facilities such as Automated Actions and Scheduled Actions that can be used to perform asynchronous processes. As I mentioned, you need to recreate the supporting structure of the ORM first before using the ORM outside. OpenERP created a special namespace which is used for importing stuff within OpenERP. If you check the openerp/osv/orm.py file, on the top you'll find lines such as import openerp.netsvc, etc. So to use orm.py file, you need to have those files as well located at the path as specified (and so on, you need to trace the other imports that are needed by those imported files respectively). I haven't tried this, but maybe installing OpenERP using the binary package might help as if you install OpenERP using binary package, it will be stored within the PYTHON PATH.

Author

The python path isn't a problem, because I can include my addons folders in sys.path, then import a custom addon. However, that does not solve the problem, the pool does not contain the custom addon's objects.

PY, sys.path is different from PYTHON PATH.

Author

I think I understand a little better. I think it's the instanciation of the pool that the addons must be added, but how, I will need to find out. (On a side note, I already now how to fix my code with xmlrpclib, so from this point, this question is just curiosity ^^)

Congratulation PY. It would be great if you can share what you have learned with all of us. And yes, the instanciation of the objects in addons and especially base must be proper to create a proper OpenERP environment.

Author

Pfff, this is a mess... I've been reading the code for more than an hour now, and I think I will pass for today ^^ I've learned a lot, but not enough. However, I will accept your answer, since it's not so easy to do it. People (including me) would be better served with xmlrpc. Thanks anyway !

Related Posts Replies Views Activity
2
Mar 15
17449
1
Mar 15
5659
2
Jul 22
5090
1
Sep 19
2242
0
Sep 15
3572