This question is my first question in stack overflow and my English is not well.I hope you can understand me.
Goal: I want to alter my form view dynamically by the record's state.
Trouble:I can't get the record or record's active_id when I into the form view from a tree view.But I can get it if I update the form view directly.
I was troubled by this problem all day. And I find some answer, but they are not detailed enough:
use read method:
@api.multi
def read(self, fields=None, load='_classic_read'):
I can't understand it.
Thanks
For now the only way to get the id of the record is to save it in the context:
i was not able to find an easy solution for you but if this is urgent you can do some thing like this:
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
action:
<record model="ir.actions.act_window" ...>
.....
.....
<field name="view_mode">tree</field>
...
</record>
tree:
<record model="ir.ui.view">
....
....
....
<tree>
...
...
<button name="open_rec" type="object" ..../>
</tree>
</record>
in model:
@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
Hope this helps you if you find a better way post it