我需要什么
如果用户点击我的按钮,根据当前记录的字段将他带到不同模型的不同 View 。
我做了什么
我已经声明了一个 object 类型的按钮来调用 Python 方法。在里面,我检查了字段。如果它的值为A,我将他重定向到模型X,如果它的值为B,我将他重定向到模型Y。
代码
@api.multi
def my_button_method(self):
self.ensure_one()
if self.my_field == 'A':
action_id = self.env.ref(
'my_module.my_ir_actions_act_window_A')
return action_id.__dict__
elif self.my_field == 'B':
action_id = self.env.ref(
'my_module.my_ir_actions_act_window_B')
else:
action_id = False
if action_id:
return action_id.__dict__
如果我在 action_id
的定义之后写这个:
_logger.info(action_id.name)
_logger.info(action_id.type)
_logger.info(action_id.res_model)
_logger.info(action_id.view_type)
_logger.info(action_id.view_mode)
_logger.info(action_id.search_view_id)
_logger.info(action_id.help)
我得到了正确的值。所以,在谷歌搜索之后,我看到 __dict__
给了你一个字典,它的键是对象的属性,它的值是这些属性的值。
但是代码给出了一个错误,因为 action_id.__dict__
没有返回一个包含预期的操作属性及其值的字典,而是返回以下内容:
{
'_prefetch': defaultdict(
<type 'set'>,
{
'ir.actions.act_window': set([449])
}
),
'env': <odoo.api.Environment object at 0x7f0aa7ed7cd0>,
'_ids': (449,)
}
谁能告诉我为什么?我试图避免这段代码:
@api.multi
def my_button_method(self):
self.ensure_one()
if self.my_field == 'A':
action_id = self.env.ref(
'my_module.my_ir_actions_act_window_A')
elif self.my_field == 'B':
action_id = self.env.ref(
'my_module.my_ir_actions_act_window_B')
else:
action_id
if action_id:
return {
'name': action_id.name,
'type': action_id.type,
'res_model': action_id.res_model,
'view_type': action_id.view_type,
'view_mode': action_id.view_mode,
'search_view_id': action_id.search_view_id,
'help': action_id.help,
}
可能有使用服务器操作的更好的解决方案,如果有的话,有人知道怎么做吗?
最佳答案
你可以做真正简短的“风格”:
action = self.env.ref('my_module.my_ir_actions_act_window_A').read()[0]
return action
您会在 Odoo 代码中找到很多这种“风格”的示例。
编辑:这也适用于 Odoo 7-14
关于python - 如何在 Odoo 10 中从 Python 返回现有的 ir.actions.act_window?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50510639/