Odoo v10 @onchange(stage_id) 不适用于 <field name="stage_id" widget="statusbar" clickable="True"/>

Odoo v10 @onchange(stage_id) doesn't work with <field name="stage_id" widget="statusbar" clickable="True"/>

Odoo v10 @onchange(stage_id) 不适用于 stage_id 小部件字段。当我在表单正文中添加字段简单字段时它起作用了,但原始字段不起作用:

<field name="stage_id" widget="statusbar" clickable="True"/>

class TaskExtension(models.Model):
    _name = 'project.task'
    _inherit = ['project.task']

    @api.model('stage_id', 'date_deadline')
    def _onchange_responsible(self):
        self.user_id = self.stage_id.responsible


    user_id = fields.Many2one('res.users',string = 'Assigned To',computed = _onchange_responsible,store=True)

on change函数应该这样写

@api.onchange('stage_id')  # triggered fields
def on_change_stage_id(self):
    # your logic here like:
    if self.stage_id:
       self.user_id = self.stage_id.responsible

If statusbar is clickable then onchange method won't call. It will directly call write method.

所以在这种情况下你需要处理write方法。

@api.multi
def write(self, vals):
    res = super(class_name, self).write(vals)
    ### your logic
    return res

如果你想调用 onchange 函数那么你可以使用 @api.onchange 装饰器。

@api.model 将在您想调用不带任何对象的任何方法(使用模型引用/空白记录集)时使用。

@api.onchange 将用于在更新一个或多个字段的值时调用函数。

@api.onchange('field1', 'field2')
def function_name(self):
    # your logic here like:

最后你的方法应该是这样的,

@api.onchange('stage_id')
def on_change_stage_id(self):
    self.user_id = self.stage_id.responsible

In odoo onchange method field must me available(visible/invisible) in the form view, otherwise it will not work.

试试这个:

<field name="state" widget="statusbar" options="{'fold_field': 'fold'}" clickable="True"/>