Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
4371 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
giu 25
15648
3
apr 25
5957
2
lug 24
4778
1
gen 24
1994
1
ott 23
2113