如何在 Odoo 10 中创建服务器操作?
How to create server actions in Odoo 10?
在 Odoo 8 中,我能够使用 XML 定义服务器操作,例如:
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="action" model="ir.actions.server">
<field name="name">My Action</field>
<field name="model_id" ref="model_module_model"/>
<field name="code">self.action(cr, uid, context=context)</field>
</record>
</data>
</odoo>
这将执行我的 module.model.action() 方法。
在 Odoo 10 中,这段代码抛出异常:
ERROR:odoo.http:Exception during JSON request handling.
Traceback (most recent call last):
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/service/model.py", line 119, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/web/controllers/main.py", line 1129, in run
result = request.env['ir.actions.server'].browse([action_id]).run()
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/base/ir/ir_actions.py", line 964, in run
res = func(action, eval_context=eval_context)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/base/ir/ir_actions.py", line 793, in run_action_code_multi
safe_eval(action.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action'
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/tools/safe_eval.py", line 301, in safe_eval
return unsafe_eval(c, globals_dict, locals_dict)
File "", line 1, in <module>
ValueError: <type 'exceptions.NameError'>: "name 'self' is not defined" while evaluating
u'self.action(cr, uid, context=context)'
我在 https://www.odoo.com/documentation/10.0/reference/actions.html#code
的 Odoo 文档中没有看到任何关于服务器操作的有趣内容
并且文档明确指出 self 是服务器操作的评估上下文的一部分,请参阅 https://www.odoo.com/documentation/10.0/reference/actions.html#reference-actions-server-context
如何在 Odoo 10 中创建服务器操作?
在 Odoo 10 中声明服务器操作时不需要 self 对象。我们可以直接使用 env[[=29 访问 model/object =]]
尝试使用以下代码:
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="action" model="ir.actions.server">
<field name="name">My Action</field>
<field name="model_id" ref="model_module_model"/>
<field name="code">
if context.get('active_model') == 'your.module.model' and context.get('active_ids'):
action = env['module.model'].browse(context['active_ids']).action()
</record>
</data>
</odoo>
编辑:
当没有active_ids[=时,我们可以使用这些env['module.model'].action()来执行方法24=]
自 Odoo 10 以来,旧的 API 已被完全删除。所以我猜您使用 cr、uid 和上下文的旧式调用是问题所在。尝试只调用模型,或者如果有要调用该方法的记录,请使用它。
# model call
model.action()
# record call
object.action()
小提示:尝试在客户端的设置下创建一个服务器动作。键入 'code' 是默认值,您将获得一些关于此功能的文档:-)
# model call
<field name="code">
model.action_name()
</field>
# record call
<field name="code">
record.action_name()
</field>
在 Odoo 8 中,我能够使用 XML 定义服务器操作,例如:
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="action" model="ir.actions.server">
<field name="name">My Action</field>
<field name="model_id" ref="model_module_model"/>
<field name="code">self.action(cr, uid, context=context)</field>
</record>
</data>
</odoo>
这将执行我的 module.model.action() 方法。
在 Odoo 10 中,这段代码抛出异常:
ERROR:odoo.http:Exception during JSON request handling.
Traceback (most recent call last):
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 638, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 675, in dispatch
result = self._call_function(**self.params)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 331, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/service/model.py", line 119, in wrapper
return f(dbname, *args, **kwargs)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 324, in checked_call
result = self.endpoint(*a, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 933, in __call__
return self.method(*args, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 504, in response_wrap
response = f(*args, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/web/controllers/main.py", line 1129, in run
result = request.env['ir.actions.server'].browse([action_id]).run()
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/base/ir/ir_actions.py", line 964, in run
res = func(action, eval_context=eval_context)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/base/ir/ir_actions.py", line 793, in run_action_code_multi
safe_eval(action.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action'
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/tools/safe_eval.py", line 301, in safe_eval
return unsafe_eval(c, globals_dict, locals_dict)
File "", line 1, in <module>
ValueError: <type 'exceptions.NameError'>: "name 'self' is not defined" while evaluating
u'self.action(cr, uid, context=context)'
我在 https://www.odoo.com/documentation/10.0/reference/actions.html#code
的 Odoo 文档中没有看到任何关于服务器操作的有趣内容并且文档明确指出 self 是服务器操作的评估上下文的一部分,请参阅 https://www.odoo.com/documentation/10.0/reference/actions.html#reference-actions-server-context
如何在 Odoo 10 中创建服务器操作?
在 Odoo 10 中声明服务器操作时不需要 self 对象。我们可以直接使用 env[[=29 访问 model/object =]]
尝试使用以下代码:
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<data>
<record id="action" model="ir.actions.server">
<field name="name">My Action</field>
<field name="model_id" ref="model_module_model"/>
<field name="code">
if context.get('active_model') == 'your.module.model' and context.get('active_ids'):
action = env['module.model'].browse(context['active_ids']).action()
</record>
</data>
</odoo>
编辑:
当没有active_ids[=时,我们可以使用这些env['module.model'].action()来执行方法24=]
自 Odoo 10 以来,旧的 API 已被完全删除。所以我猜您使用 cr、uid 和上下文的旧式调用是问题所在。尝试只调用模型,或者如果有要调用该方法的记录,请使用它。
# model call
model.action()
# record call
object.action()
小提示:尝试在客户端的设置下创建一个服务器动作。键入 'code' 是默认值,您将获得一些关于此功能的文档:-)
# model call
<field name="code">
model.action_name()
</field>
# record call
<field name="code">
record.action_name()
</field>