تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
4 الردود
9808 أدوات العرض

I am trying to enter value in read only field , it is value changed no problem but when I save date it's value not changed . 

How can I make it?

الصورة الرمزية
إهمال

By default you're not allowed to save data in readonly fields, that is the whole purpose of the field.

أفضل إجابة

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

الصورة الرمزية
إهمال
أفضل إجابة

Mohammed,

By design, the readonly field's value will not be sent out to the server from the client.

If your readonly field is functional field, its value will be automatically stored on screen. If its storable function field, you must make sure to write the right method to calc its value.

If your fieldis not functional, you should work around by onchange and set one invisible field to hold the value of the readonly field. Or, use write well.

Thanks.

الصورة الرمزية
إهمال
أفضل إجابة

Hi,   maybe with using a functional field and set store=True in field option!

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
1
مايو 21
4042
1
يناير 18
9183
1
فبراير 17
5113
0
يناير 17
4350
0
ديسمبر 16
2987