Odoo Help

Welcome!

This community 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.

0

How to save values in a readonly field?

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

1

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

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