Help

欢迎!

该社区面向专业人士和我们产品和服务的爱好者。
分享和讨论最好的内容和新的营销理念,建立您的专业形象,一起成为更好的营销人员。


0

Readonly field in onchange method not store value when save

Avatar
odoo
1 Comment
Avatar
Discard
4 Answers
0
Avatar
odoo
Best Answer

Found Solution!

use attribute force_save="1" in view to save value of readonly field.


<field name="payment_type" readonly="1" force_save="1"/>

it will save value of payment_type field when onchange trigger.



5 Comments
Avatar
Discard
Avatar
odoo
-

so Helpful, thank u sir

Avatar
odoo
-

Awesome! Thank you!

Avatar
odoo
-

You've been so helpful! Thank you!

Avatar
odoo
-

customer = fields.Many2one( 'res.partner', string='Customer', readonly="1", force_save="1")
NOT WORKING

Avatar
odoo
-

@Fawad Mazhar add it in the same field but in XML view not in Python field

0
Avatar
odoo
Best Answer

I did it the following way, correct me if I am wrong, I forced saved the value. It works just fine. 

"<xpath expr="//field[@name='amount_total']" position="attributes">

<attribute name="force_save">True</attribute>

</xpath>"

Avatar
Discard
0
Avatar
odoo
Best Answer

Hi all,

The best solution is to add the attribute force_save="1" for the field in XML file.

We no need to install any third party module.

Hope it helps!

Avatar
Discard
0
Avatar
odoo
Best Answer

Hello

I hope below code will help you

In my case have Total year field is readonly and based on 'Date of birth' Total year will be update

Using onchange method, Can get Total year on field but when save that record Total Year field to set blank

Solution:-

Create new dummy field of total year and set that dummy field value on original field

Example:-

Python file

total_year = fields.Float()

total_year_copy = fields.Float()

from datetime import date​​

# Onchange method

@api.onchange('dob')
def onchange_dob(self):
today = date.today()
self.total_year = self.total_year_copy = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day))

# Create method

@api.model

def create(self, vals):

if 'total_year_copy' in vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).create(vals)

# Write method

@api.multi

def write(self, vals):

if 'total_year_copy' in vals:

vals.update({'total_year': vals.get('total_year_copy')})

return super(Project, self).write(vals)

Xml File

<field name="total_year" readonly="1"/>

<field name="total_year_copy" invisible="1"/>

Hope this help you to save readonly records

Best Regards,

Ankit H Gandhi

Avatar
Discard