如何在 Odoo 10 中 return 来自 Python 的现有 ir.actions.act_window?

How to return an existing ir.actions.act_window from Python in Odoo 10?

我需要的

如果用户点击我的按钮,根据当前记录的某个字段将他带到不同模型的不同视图。

我做了什么

我已经声明了一个类型 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__ 没有 return 具有预期的操作属性及其值的字典,而是以下内容:

{
    '_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