跳至内容
菜单
此问题已终结
6 回复
49690 查看

I change the value of a readonly field in onchange method.
Its working properly when onchange trigger.

when i click on save button it will not be stored in the database.

any solution without using third party module or overriding create or write method?


形象
丢弃
编写者 最佳答案

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.



形象
丢弃

so Helpful, thank u sir

Awesome! Thank you!

You've been so helpful! Thank you!

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

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

Good Answer !

Worked For Me, Thanks!

最佳答案

You can use force_save in xml file to save data in readonly filed populated by onchange method.

形象
丢弃
最佳答案

This means the user cannot enter data in this field. When a field is read-only, it can only be viewed and cannot be modified.
name="versand_route_id" readonly="1"/>

If this field can change dynamically depending on another field and you want to save its content, you can use force_save="1".

name="versand_route_id" readonly="1" force_save="1"/> 


形象
丢弃
最佳答案

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

形象
丢弃

you are not wrong. your method is depend on the field that you want to force_save. If the field is in the odoo-server framework, you have to make your own module and put this in your xml.
Q : Can't I just edit the module in odoo-server?
A : Yes, you can. But I think we should not touch any module in odoo-server

if the field is in your own module, the accepted answer is better

最佳答案

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!

形象
丢弃
最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
1
2月 23
697
0
2月 23
724
2
9月 20
18098
1
7月 19
3425
4
6月 19
10595