Odoo Help

0

[@api.onchange] change state fields value

By
Othmane Ghandi
on 5/9/17, 6:00 AM 2,996 views

Hi Community

I wrote an onchange method that change the state field value based on the value of another field.

 

@api.multi
@api.onchange('lgs')
def on_change_state(self):
values = {}
for record in self:
if record.lgs:
values = {'state': 'waiting'}
else:
values = {'state': 'draft'}
print self.state, self.lgs
return {'value': values}
I see that the "state" value change. But when I save the form the state field take the old version.
Please How can I resolve this issue. I think this issue is only for state field
0

Yenthe

--Yenthe--
10105
| 8 8 8
Belgium
--Yenthe--

My Odoo blog: http://www.odoo.yenthevg.com/

My Twitter: https://twitter.com/Yenthe666

Yenthe
On 5/9/17, 6:51 AM

Hi Othmane,


Why do you have @api.multi above the @api.onchange? They should not be combined, you only need the onchange.
As for the value assigning: why don't you simply write on self right away? Your code should look like this:

@api.onchange(lgs)
def on_change_state(self):
    for record in self:
if record.lgs:
record.state = 'waiting'
else:
record.state = 'draft'
print self.state, self.lgs


Regards,

Yenthe

@api.multi above the @api.onchange They should not be combined: You are right !! thanks !!

But it's working hhh

Othmane Ghandi
on 5/9/17, 6:56 AM
0

Mohammed Amal

--Mohammed Amal--

2009
| 5 3 7
Malappuram, India
--Mohammed Amal--

Email : mohammedamal.n@gmail.com

Mohammed Amal
On 5/9/17, 6:48 AM

You are not writing values to self,

Try this  self.write(values)

I tried it but it doesn't solve my problem :(

Othmane Ghandi
on 5/9/17, 7:09 AM
0
Othmane Ghandi
On 5/9/17, 6:54 AM

Mohammed Amal

I tried it but it didn't work.

I got the answer from a colleague "Maniganda" : in my case I removed the " readonly=True " attribute of the state fields and it work !!
 

0
zahid
On 5/9/17, 6:56 AM

onchange method does not need to retrun a value. 

you can do this 

@api.multi
@api.onchange('lgs')
def on_change_state(self):
 
for record in self:
if record.lgs:
record.state = 'waiting'
else:
record.state = 'draft'

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 5/9/17, 6:00 AM
Seen: 2996 times
Last updated: 6/19/18, 2:51 AM