Ir al contenido
Menú
Se marcó esta pregunta
4 Respuestas
17155 Vistas

​​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
Descartar
Mejor respuesta

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
Descartar
Autor

Nice idea! Thanks, I'll try it.

Mejor respuesta

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
Descartar
Mejor respuesta

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
Descartar
Mejor respuesta

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
Descartar

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

Publicaciones relacionadas Respuestas Vistas Actividad
4
oct 18
12962
0
dic 24
715
1
oct 23
1294
2
oct 23
1601
2
ago 23
3338