python - How to return an existing ir.actions.act_window from Python in Odoo 10?
问题描述
What I need
If the user clicks on my button, take him to different views of different models depending on a field of the current record.
What I did
I have declared a type object button to call a Python method. Inside it, I check the field. If its value is A, I redirect him to the model X and if its value is B, I redirect him to the model Y.
The code
@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__
If I write this after the definition of 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)
I get the right values. So, after googling I saw that __dict__
gives you a dictionary whose keys are the attributes of the object and whose values the values of those attributes.
But the code is giving an error because action_id.__dict__
doesn't return a dictionary with the expected attributes of the action and its values, but the following instead:
{
'_prefetch': defaultdict(
<type 'set'>,
{
'ir.actions.act_window': set([449])
}
),
'env': <odoo.api.Environment object at 0x7f0aa7ed7cd0>,
'_ids': (449,)
}
Can anyone tell me why? I'm trying to avoid this code:
@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,
}
May be there is a much better solution using a server action, if so, does anyone know how?
解决方案
你可以做很短的“风格”:
action = self.env.ref('my_module.my_ir_actions_act_window_A').read()[0]
return action
您会在 Odoo 代码中找到很多这种“风格”的示例。
编辑:这也应该适用于 Odoo 7-14
推荐阅读
- ssh - 如何在ansible中使用“when”条件过滤输出
- php - Guzzle http 和 Laravel Http post 方法替换为 get 方法。405 方法不允许
- java - 我如何在 PrintWriter Java 中打印此文字
- python - 如何从树莓派 3b+ 上的 MCP3202 读取 SPI 值?
- c# - 将 SQL 查询转换为 LINQ
- swift - 动态关闭工作表错误后无法单击 NavigationBarItems?
- docker - 如何在 doke-compose 中动态获取网络的网关 ip?
- python - 如何使用 Python 和 Selenium 单击此元素?
- deck.gl - 如何过滤deck.gl TripsLayer上的数据
- php - 使用两个 laravel eloquent 模型对象创建一个嵌套的 JSON 对象