Перейти к содержимому
Меню
Чтобы взаимодействовать с сообществом, необходимо зарегистрироваться.
Этот вопрос был отмечен
4 Ответы
17774 Представления

​​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
Аватар
Отменить
Лучший ответ

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
Аватар
Отменить
Автор

Nice idea! Thanks, I'll try it.

Лучший ответ

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

Аватар
Отменить
Лучший ответ

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


Аватар
Отменить
Лучший ответ

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)

Аватар
Отменить

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

Related Posts Ответы Просмотры Активность
4
окт. 18
13482
0
дек. 24
1185
1
окт. 23
1977
2
окт. 23
2170
2
авг. 23
4194