Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
3 Risposte
9176 Visualizzazioni

```

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
Abbandona
Risposta migliore

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
Abbandona
Autore

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.

Autore

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.

Autore

this is close to fulfill my requirement

Cool! Please revert if you completed it!

Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
mar 20
3672
0
mar 22
1774
0
apr 20
2385
2
dic 19
2784
2
dic 19
7464