Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
4 ตอบกลับ
54812 มุมมอง

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
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

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

คำตอบที่ดีที่สุด

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'
อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

You are not writing values to self,

Try this  self.write(values)

อวตาร
ละทิ้ง
ผู้เขียน

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

ผู้เขียน คำตอบที่ดีที่สุด

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 !!
 

อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
1
ก.ย. 16
9141
1
ส.ค. 24
2931
1
พ.ย. 22
2245
2
พ.ค. 22
14582
1
เม.ย. 21
3358