Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
4 Antworten
17399 Ansichten

​​Hello,

I have a wizard form with several fields (let's call them A and B) that trigger an onchange function. I have another field (let's call it C) with its own onchange function, that may also update A and B (depending on certain criteria):

@api.onchange('a','b')
def onchange_ab(self):
self.do_something()

@api.onchange('c')
def onchange_c(self):
if self.some_criteria:
self.a = False #triggers onchange_ab
self.b = False #triggers onchange_ab again

The problem is that when I change C, and both A and B are updated, A and B's onchange function triggers twice. This module is meant to manage a sizeable amount of data so triggering it twice is indeed quite expensive, so I want to update both A and B first, and then trigger the onchange function only once. Is this possible?

Thanks.

P.S. I tried self.write({'a': False, 'b': False}) to no avail
Avatar
Verwerfen
Beste Antwort

Pass a context in the "c" and check it in "ab": 


@api.onchange('a','b')
def onchange_ab(self):
if not self.env.context.get("noonchange"):
self.do_something()

@api.onchange('c')
def onchange_c(self):
if self.some_criteria:
self.with_context(noonchange=True).a = False # pass context
self.b = False # do not pass context
Avatar
Verwerfen
Autor

Nice idea! Thanks, I'll try it.

Beste Antwort

A workaround is to put the context in the view, e.g:

<field name="firstname" context="{'changed_firstname': True}"/>


Then in python onchange method, you can check for the existence of this context value:
if self.env.context.get('changed_firstname'):
    return # skipped to avoid loops

Avatar
Verwerfen
Beste Antwort

You can cherry-pick this commit for odoo 14.0

https://github\.com/decgroupe/odoo\-ocb/commit/88111f4928bc8dbcb17987599739758885889093

Basically\ it\ adds\ two\ new\ attributes: _onchange_sender and\ _onchange_origin
You\ can\ see\ usage\ of\ this\ feature\ here:

https://github.com/odoo/odoo/pull/59173#issue-715403181


Avatar
Verwerfen
Beste Antwort

I don't know why but for me this isn't working, and even if I set context variable that's not passed to the second onchange event (Odoo v14)

Avatar
Verwerfen

Hello, it's really difficult to explain the problem here, but it's because of @api.onchange...

The best way to do it is :
self.env.context = self.with_context(noonchange=True).env.context

More info here : https://github.com/odoo/odoo/issues/7472

Verknüpfte Beiträge Antworten Ansichten Aktivität
4
Okt. 18
13208
0
Dez. 24
879
1
Okt. 23
1598
2
Okt. 23
1846
2
Aug. 23
3707