تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
3 الردود
9266 أدوات العرض

```

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
مارس 20
3717
0
مارس 22
1892
0
أبريل 20
2444
2
ديسمبر 19
2821
2
ديسمبر 19
7612