2
votes

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:

  1. Add the selected record's id to the context when someone clicked a record in the tree view, and you can get the id in fields_view_get method by context.But the answer doesn't tell us how to add the id to the context when I click the record in the tree view.
  2. use read method:

    @api.multi def read(self, fields=None, load='_classic_read'):

    I can't understand it.

Thanks

1
in a tree view you cant get active_id but active_ids.Rinaldi
And how can I get the id of the selected record?赵新岭

1 Answers

2
votes

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