Odoo Help

0

How to save values in a readonly field?

By
paolo
on 9/18/14, 8:20 AM 4,423 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.

2
Sudhir Arya (SA)
On 9/18/14, 2:22 PM

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)

Thanks, I will try :) thanks a lot

paolo
on 9/18/14, 10:56 PM

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} #----------------------------------------------------

Jose Gpe Osuna
on 7/27/15, 9:05 PM
0
Jose Gpe Osuna
On 7/27/15, 9:09 PM
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}
#----------------------------------------------------

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 9/18/14, 8:20 AM
Seen: 4423 times
Last updated: 7/27/15, 9:09 PM