'Odoo. api.onchange doesn't update custom field in screen
I'm trying to override an onchange function of account.move.line model in order to update the value of a custom field I've added to the account. move model:
class SoftOneInvoiceLine(models.Model):
_inherit= "account.move.line"
change_from_user=fields.Boolean('Change by user',default=True)
@api.onchange('quantity', 'discount', 'price_unit', 'tax_ids')
def _onchange_price_subtotal(self):
super(SoftOneInvoiceLine,self)._onchange_price_subtotal()
for rec in self:
if (rec.change_from_user):
rec.move_id.global_discount=0
rec.move_id.write({'global_discount':0})
print (rec.move_id," ",rec.move_id.global_discount)
rec.change_from_user=True
The code above, doesn't change the value of the global_discount field in the form when you change one of the 4 fields of account.move.line.
The _onchange_price_subtotal functions gets executed, because the print command is displayed in the logs
Why doesn't global_discount new value doesn't reflect on UI?
Solution 1:[1]
Instead of
rec.move_id.write({'global_discount':0})
have you tried update ?
rec.move_id.update({'global_discount':0})
Also, what you meant by not reflecting in UI ? Is it updated in db table ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
