我需要什么
如果用户单击“我的”按钮,根据当前记录的字段,将他带到不同模型的不同视图。在
我做了什么
我声明了一个typeobject按钮来调用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
的定义之后写下这个:
我得到了正确的价值观。所以,在google上搜索之后,我看到__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,
}
也许有更好的解决方案使用服务器操作,如果是,有人知道怎么做吗?在
你可以做很短的“风格”:
你会在Odoo代码中找到很多这种“风格”的例子。在
相关问题 更多 >
编程相关推荐