跳至内容
菜单
此问题已终结
3 回复
9061 查看

```

operating_unit = fields.Many2one('operating.unit', string='Operating Unit', default=lambda self: self._get_default_opr_unit())

@api.model 

 def _get_default_opr_unit(self): 

 return self.env['ir.default'].sudo().get('res.config.settings', 'operating_unit' or False)


@api.multi 

 @api.onchange('operating_unit') 

def _operating_unit_onchange(self): 

 if self.operating_unit: 

 return {'domain': {'adjustment_journal_id': [('operating_unit_id.id', '=', self.operating_unit.id)]}} 

 else: return {'domain': {'adjustment_journal_id': [('operating_unit_id.id', '=', False)]}}


```

when i load my wizard default value fill correctly but auto run my onchange method why ? 
i want to run my method when i change my field not auto  

where i'm wrong ?

形象
丢弃
最佳答案

Hi, 

Your onchange method depends upon the operating_unit field. Since a default value is being loaded to the operating_unit, there is a change in value which will trigger the onchange method. So whenever the value in that field changes, even when the default value is loaded, it will trigger the onchange method.

形象
丢弃
编写者

may i have other way load default value first then my on change method run ? any logic

The onchange method will run if you are passing the default value. You can check whether the onchange is triggered for the first time and if yes, don't execute the remaining part.

编写者

how i know it's run first time ?

Try creating a new invisible field without any value. In your onchange check whether this field is empty or not. If it is empty, write some value to this field and exit. If it is not empty continue with your onchange. I mentioned this because this is easy to understand. There may be better ways to accomplish the same thing.

编写者

this is close to fulfill my requirement

Cool! Please revert if you completed it!

最佳答案

When default method is launched, it changes the field value: false > default value. 

This operation is caught by @api.onchange. Hence, your onchange method is triggered.

There is now way to avoid this, rather than add some extra checks in your onchange. Perhaps, some check of a previous value (using self._origin).

形象
丢弃
相关帖文 回复 查看 活动
1
3月 20
3601
0
3月 22
1708
0
4月 20
2311
2
12月 19
2703
2
12月 19
7375