Pular para o conteúdo
Menu
Esta pergunta foi sinalizada
1 Responder
4378 Visualizações

I spent a lot of time understanding the behaviour of @api.onchange in some of my forms. I found on my own that it was due to the name "length" given to a computed field. As example, here is a very simple model for words, with a field that computes the length and an onchange function to ensure that the words are recorded in uppercase.

class mymodule_words(models.Model): 

    _name = 'mymodule.words' 
    word = fields.Text('Word')
    length = fields.Integer('Length', compute='_compute_length')

    @api.depends('word')
    def _compute_length(self): 
        for record in self:
            if not record.word: record.word = ""
            record.length = len(record.word)

    @api.onchange('word')
    def _onchange_word(self):
        self.word = self.word.upper() 

In odoo12, using this model,  the onchange function that shoud convert words to uppercase is not working as expected (very erratic behaviour).

I just replaced "length" as field name by the french word "longueur". Then, onchange works perfectly.

Should it be reported as a bug ? I believe so. Until it is fixed by Odoo, "length" should be avoided as field name, although I haven't seen in any Odoo documentation that it was a reserved name.

Pierre-François Berne

Yubsis


Avatar
Cancelar
Melhor resposta

I have the same problem with the onchange and length field (mine is a normal Float field):

_inherit = 'product.template'

inchs = fields.Boolean('Inchs?', default=False)
thickness = fields.Float('Thickness')
width = fields.Float('Width')
length = fields.Float('Length')

@api.onchange('inchs')
def _onchange_inchs(self):
    self.update({
        'length': 0.0,
        'width': 0.0,
        'thickness': 0.0
   })

I followed your advice and change the name to lng and it worked without problems, I thought it was my mistake, but I can rest assured that it is Odoo.

David Acevedo Toledo

Amaris Consulting

Avatar
Cancelar
Publicações relacionadas Respostas Visualizações Atividade
1
jun. 25
15672
3
abr. 25
5981
2
jul. 24
5098
1
jan. 24
2012
1
out. 23
2121