Skip to Content
Menu
This question has been flagged
3 Replies
8316 Views

```

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 ?

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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.

Author

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.

Author

this is close to fulfill my requirement

Cool! Please revert if you completed it!

Best Answer

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).

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 20
3056
0
Mar 22
1283
0
Apr 20
1951
2
Dec 19
2324
2
Dec 19
6673