Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
3 Antwoorden
9071 Weergaven

```

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
Annuleer
Beste antwoord

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
Annuleer
Auteur

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.

Auteur

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.

Auteur

this is close to fulfill my requirement

Cool! Please revert if you completed it!

Beste antwoord

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
Annuleer
Gerelateerde posts Antwoorden Weergaven Activiteit
1
mrt. 20
3604
0
mrt. 22
1716
0
apr. 20
2312
2
dec. 19
2709
2
dec. 19
7381