Search code examples
pythonodooonchange

Odoo onchange method is not saving values in readonly fields


In my view I have a Boolean field (chk_valido) and two other fields. I want those fields to get the value of the current user and the current date, when the boolean fields is set to True. With this code the fields are getting the value but when I click the "Save" button those values desapear. What is causing these issue? How can i save those values? "user_valido" and "fecha_validaciongabinete" are set to readoly in the view

chk_valido = fields.Boolean(string='Está validado')
user_valido = fields.Many2one('res.users', string='Usuario valido')
fecha_validaciongabinete = fields.Datetime(string='Fecha Validacion gabinete')

@api.onchange('chk_valido')
def _onchange_chk_valido(self):
    if self.chk_valido:
        self.fecha_validaciongabinete=fields.Datetime.today()
        self.user_valido= self.env.user
    else:
        self.fecha_validaciongabinete=""
        self.user_valido=False

Solution

  • I found a solution: adding force_save="1" to the readonly fields in the view. I would be like this:

    <field name="user_valido" readonly="1" force_save="1"/>
    
    <field name="fecha_validaciongabinete" readonly="1" force_save="1"/>