This question has been flagged
7 Replies
19437 Views

i created a on_change function, that changes the value of a certain field float. it does change its value but when i save it, the values reverts to default. in my case 0. 

i tried removing the readonly, and it works. But the thing is that i need it to be a readonly so it can't be edited by the user.

Avatar
Discard
Best Answer

Simple...
set the force_save to 1 to allow on_change events to be able to save on the server side.

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

Avatar
Discard
Best Answer

Onchange method doesn't pass the values on server side, it remains on client side until you save the record.

But this doesn't work on readonly fields. For that you have to write a tricky code.

You have to override create and write methods in which you can call your onchange method that will return a dictionary from which you can fetch the value of your readonly field and pass it in dictionary of create or write method.

So user will be able to see onchange effect on readonly field and the value will be saved on database with your tricky code.

Sample:

def write(cr, uid, ids, vals, context=None):
    for rec in self.browse(cr, uid, ids, context=context):
        #Pass required value of fields in onchange
        res = self.your_onchange(cr, uid, [rec.id], rec.field1, rec.field2)
        if res.has_key('values'):
            vals.udate({'your_readonly_fields_value': res.get('your_readonly_fields_value')})
    return super(your_class_name, self).write(cr, uid, ids, vals, context=context)

def create(cr, uid, vals, context=None):
    #Pass required value of fields in onchange
    res = self.your_onchange(cr, uid, [], vals.get('field1'), vals.get('field2'))
    if res.has_key('values'):
        vals.udate({'your_readonly_fields_value': res.get('your_readonly_fields_value')})
    return super(your_class_name, self).create(cr, uid, vals, context=context)

Avatar
Discard
Author

Thanks, I will try :) thanks a lot

Because my onchange function is pretty elaborated, I didn't want to execute it again when saving/creating data, so I decided to use the self.write at the end of the onchange function. Something like this: #-------------------------------------------------- your_onchange(cr, uid, ids, field1, field2, context=None): context = context or {} vals = {} .... .... # All my elaborated stuff to get my_new_value... .... vals.update ( {'your_readonly_fields_value': my_new_value } ) self.write( cr, uid, ids, vals, context ) return {'value': vals} #----------------------------------------------------

Best Answer
The Sudhir Arya (SA) worked for me, but my onchange function is pretty elaborated, so I didn't want to execute it again when saving/creating data, so I decided to use a self.write at the end of the onchange function. Something like this:

#--------------------------------------------------

your_onchange(cr, uid, ids, field1, field2, context=None):
   context = context or {}
   vals = {}
   ....
   .... # All my elaborated stuff to get my_new_value...
   ....
   vals.update ( {'your_readonly_fields_value': my_new_value } )
   self.write( cr, uid, ids, vals, context )
   return {'value': vals}
#----------------------------------------------------

Avatar
Discard
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
Best Answer

my solution:

change readonly to invisible:

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

add related field:

    product_to_show = fields.Many2one(
        'product.product',
        readonly=True,
        related='product_id'
    )


show related:

<field name="product_to_show"/>

Avatar
Discard