Ir al contenido
Menú
Se marcó esta pregunta
8 Respuestas
18158 Vistas

Hi

How to store field readonly in database with onchange method

my code :

 def fret_change (self,cr, uid,ids ,tva_p,t_c,prix_d,fret_p,context):
        vals={}
        vals['p_achat_ht']= (t_c*prix_d)*(1+(fret_p/100))
         
        return {'value': vals}

def write(self,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.fret_change(cr, uid, [rec.id], rec.tva_p, rec.t_c,rec.prix_d,rec.fret_p,context)
            if res.has_key('p_achat_ht'):
                vals.update({'p_achat_ht': res.get('p_achat_ht')})
        return super(product_product, self).write(cr, uid, ids, vals, context=context)

    def create(self,cr, uid, vals, context=None):
        #Pass required value of fields in onchange
        res = self.fret_change(cr, uid, [], vals.get('tva_p'),vals.get('t_c'), vals.get('prix_d'),vals.get('fret_p'),context)
        if res.has_key('p_achat_ht'):
            vals.update({'p_achat_ht': res.get('p_achat_ht')})
        return super(product_product, self).create(cr, uid, vals, context=context)

thanks

Avatar
Descartar
Mejor respuesta

Hey! guys. We found a solution to store readonlyonchange in DB through below code.. CREATE edit, it will store in DB

samp_cal1=fields.Integer(string="a")

samp_cal2=fields.Integer(string="b")

samp_cal3=fields.Integer(string="c",readonly=True)

@api.onchange('samp_cal1','samp_cal2')

def adding_samp(self):

self.samp_cal3=self.samp_cal1+self.samp_cal2

@api.model

def create(self,vals):

    if self.samp_cal3:

        vals['samp_cal3']=self.samp_cal1+self.samp_cal2

    res = super(tokenreservation, self).create(vals)

    return res

@api.multi

def write(self,vals):

    if not self.samp_cal3:

        vals['samp_cal3'] = self.samp_cal1+self.samp_cal2

    res = super(tokenreservation, self).write(vals)

    return res

Avatar
Descartar
Mejor respuesta

Hi friend, Usually when field is readonly and it gets input through on_change function after saving the record the value wont stored in database.So try below code:

addons/web/static/js/view_form.js:

In view_form.js:

in line: 840:

if (!f.get("readonly")) {

     values[f.name] = f.get_value(); }

else { readonly_values[f.name] = f.get_value(); }

     values[f.name] = f.get_value(); 

if (f.get("readonly")) 

   readonly_values[f.name] = f.get_value();

}

}

I think it will help you Thanks

Avatar
Descartar

Can you format your answer code more nicely. I think your solution could works.

Mejor respuesta

Dear All,

Kindly look at this blog post for solution https://bharatrdevnani.wordpress.com/2016/02/04/readonly-attributes-onchange-odoo/ surely it will help.


Regards,

Bharat

Avatar
Descartar
Mejor respuesta

Hi

Try this : http://www.odooninja.com/readonly-field-onchange-method/

i try it its OK

Avatar
Descartar
Mejor respuesta

As the term indicates Readonly, meaning values are not changed in those fields, hence odoo will not consider those values while saving it into database....

So best way to handle is making it as Functional / Computational field... that way it will be readonly and as well value will computated and can be either stored or not-stored...

But still you wish to use basic/normal fields with readonly property, then in ORM methods both create & write method, call the onchange event and update your values for those readonly fields manually...

 

Avatar
Descartar

hey Dep..! we can change onchange readonly field and save it to DB.. look my code

Mejor respuesta

If the field in new module you created you can making it functional field, but if the field already on system created by OpenERP/Odoo  don't change the field just create new field and save the value on it

Avatar
Descartar