• Tek Siong, Hock

Odoo Technical - update a field upon a change in another field

This Odoo technical development blog is my way to contribute back to the Odoo Community, for all the selfless and great sharing by the community members.


Recently, I have a scenario to update my custom model's value when invoice is paid (ie, upon user registered a payment, the invoice's state will be changed to Paid).

There are a few options for me to update a field upon a change in another field.


1) @api.onchange

This is the most intuitive method.

You may use it when the field to be updated is in the same model and same form/view.


If i used the @api.onchange('state') in the inherited 'Account.invoice' model, it will not be triggered.

2) @api.depends

You may use it with the compute method.

This method is triggered during the loading of the related view/form, but not when the invoice state is changed to Paid.


3) write method

This works when inherited the model account.invoice.

Triggered every time when the record is changed, after the record is created.

vals will contain the field that has been changed.


Click "Like" at the bottom of this blog, to motivate us to continue sharing more Odoo tips.

@api.multi
def write(self, vals):
    res = super(AccountInvoice, self).write(vals)
    #update your custom model's field when the Invoice state is paid
    state = vals.get("state")
    if state == 'paid':
        #update your custom model
    return res

139 views

©2019 by Excelroot Technology Sdn Bhd.