我正在尝试通过XMLRPC远程执行模块方法。我在文档中找不到任何有效的例子:
models.execute_kw(db, uid, password,
'res.partner', 'check_access_rights',
['read'], {'raise_exception': False})
如果我想执行以下方法该怎么办:
@api.model
def foo():
....
或者这个:
@api.multi
def bar(self):
....
具体来说,我尝试了一些简单的事情。我将stock.picking
扩展为:
class Picking(models.Model):
_inherit = 'stock.picking'
@api.model
def test():
print 'debug test'
return []
以下连接的python代码:
import xmlrpclib
srv = 'http://localhost:8069'
db = 'mydb'
user, pwd = 'info@foo.com', 'password'
common = xmlrpclib.ServerProxy('%s/xmlrpc/2/common' % srv)
print common.version()
uid = common.authenticate(db, user, pwd, {})
print uid
api = xmlrpclib.ServerProxy('%s/xmlrpc/2/object' % srv)
result = api.execute_kw(db, uid, pwd, 'stock.picking', 'test', [])
print result
以下错误消息是:
# python test2.py
{'server_version_info': [10, 0, 0, 'final', 0, ''], 'server_serie': '10.0', 'server_version': '10.0-20171030', 'protocol_version': 1}
1
Traceback (most recent call last):
File "test2.py", line 12, in <module>
result = api.execute_kw(db, uid, pwd, 'stock.picking', 'test', [])
File "/usr/lib/python2.7/xmlrpclib.py", line 1243, in __call__
return self.__send(self.__name, args)
File "/usr/lib/python2.7/xmlrpclib.py", line 1602, in __request
verbose=self.__verbose
File "/usr/lib/python2.7/xmlrpclib.py", line 1283, in request
return self.single_request(host, handler, request_body, verbose)
File "/usr/lib/python2.7/xmlrpclib.py", line 1316, in single_request
return self.parse_response(response)
File "/usr/lib/python2.7/xmlrpclib.py", line 1493, in parse_response
return u.close()
File "/usr/lib/python2.7/xmlrpclib.py", line 800, in close
raise Fault(**self._stack[0])
xmlrpclib.Fault: <Fault 1: 'Traceback (most recent call last):\n File "/usr/lib/python2.7/dist-packages/odoo/service/wsgi_server.py", line 56, in xmlrpc_return\n result = odoo.http.dispatch_rpc(service, method, params)\n File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 118, in dispatch_rpc\n result = dispatch(method, params)\n File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 38, in dispatch\n res = fn(db, uid, *params)\n File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 157, in execute_kw\n return execute(db, uid, obj, method, *args, **kw or {})\n File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 101, in wrapper\n return f(dbname, *args, **kwargs)\n File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 164, in execute\n res = execute_cr(cr, uid, obj, method, *args, **kw)\n File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 153, in execute_cr\n return odoo.api.call_kw(recs, method, args, kw)\n File "/usr/lib/python2.7/dist-packages/odoo/api.py", line 687, in call_kw\n return call_kw_model(method, model, args, kwargs)\n File "/usr/lib/python2.7/dist-packages/odoo/api.py", line 669, in call_kw_model\n context, args, kwargs = split_context(method, args, kwargs)\n File "/usr/lib/python2.7/dist-packages/odoo/api.py", line 296, in split_context\n return args[pos], args[:pos], kwargs\nIndexError: tuple index out of range\n'>
答案 0 :(得分:1)
我相信你对这一行有一个问题:
result = api.execute_kw(db, uid, pwd, 'stock.picking', 'test', [])
应该是:
result = xmlrpclib.execute_kw(db, uid, pwd, 'stock.picking', 'test', [])
只需将api
替换为xmlrpclib