当我从树视图进入表单视图时,如何在 fields_view_get 方法中获取记录

How can I get the record in fields_view_get method when I into the form view from a tree view

这个问题是我stack overflow的第一个问题,我的英文不是well.I希望你能理解我。

目标:我想根据记录的状态动态改变我的表单视图。

问题:从树进入表单视图时无法获取记录或记录的active_idview.But直接更新表单视图可以获取

被这个问题困扰了一整天。我找到了一些答案,但它们不够详细:

  1. 当有人在树视图中点击记录时,将所选记录的 id 添加到上下文中,您可以通过 context.But 在 fields_view_get 方法中获取 id 答案没有告诉我们如何当我在树视图中单击记录时将 id 添加到上下文中。
  2. 使用读取方法:

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

    看不懂

谢谢

目前获取记录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

如果您找到更好的方法,希望这对您有所帮助 post