这个问题是我在堆栈溢出中的第一个问题,我的英语不太好。我希望你能理解我。
目标:我想通过记录的状态动态更改表单视图。
麻烦:当我从树视图进入表单视图时,我无法获取记录或记录的active_id。但是如果我直接更新表单视图,我就可以得到它。
我整天都对这个问题感到困扰。我找到了一些答案,但它们不够详细:
使用read方法:
@api.multi
def read(self, fields=None, load='_classic_read'):
我无法理解。
由于
答案 0 :(得分:2)
现在获取记录ID的唯一方法是保存 它在上下文中:
我无法为您找到一个简单的解决方案,但如果这很紧急的话 你可以这样做:
1 - define an action that opens the record in tree or kanban view without form
2 - add a button in the tree view to force the use to open the record from there if
he want to edit it.
3 - that button calls a method in your model to open that record in form view
4 - in the context add the id of that record with special key
5 - in your fields_view_get check if that key is in the context and change the form
arch from there
动作:
<record model="ir.actions.act_window" ...>
.....
.....
<field name="view_mode">tree</field>
...
</record>
树:
<record model="ir.ui.view">
....
....
....
<tree>
...
...
<button name="open_rec" type="object" ..../>
</tree>
</record>
模特中的:
@api.multi
def open_rec(self):
# return an window action
form_id = self.env.ref('module_name.form_xml_id').id
context = self.env.context
context.update({'current_rec': self.id} # change context to add rec id
return {
'name': _('Title'),
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'model.name',
'view_id': form_id,
'target': 'current',
'context': context,
}
@api.model
def fields_view_get(self, view_id=None, view_type='form', toolbar=False, submenu=False):
result = super(YourClass, self).fields_view_get(view_id, view_typ, toolbar, submenu)
if context.get('current_rec') and view_type='form':
# this is when you need to change the resutl
希望如果您找到更好的发布方式,这会对您有所帮助