Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
4 Trả lời
17777 Lượt xem

​​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
Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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
Ảnh đại diện
Huỷ bỏ
Tác giả

Nice idea! Thanks, I'll try it.

Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

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)

Ảnh đại diện
Huỷ bỏ

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

Bài viết liên quan Trả lời Lượt xem Hoạt động
4
thg 10 18
13484
0
thg 12 24
1185
1
thg 10 23
1977
2
thg 10 23
2173
2
thg 8 23
4197