Skip to Content
Menu
This question has been flagged
1 Reply
3514 Views

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
Discard
Best Answer

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
Discard
Related Posts Replies Views Activity
2
Jul 24
414
1
Jan 24
424
2
Apr 24
1952
1
Oct 23
364
2
Oct 23
678