Search code examples
pythonodooodoo-17

Odoo onchange event for invoice_status of sale.order not firing


Does anyone know why the onchange event as coded below is not fired as expected? It fires only once, when making a new Sale Order (before saving). The invoice_status is actually changing (as is confirmable in the tree view). Expected behavior is that the event is fired on every change.

import logging
from odoo import api, fields, models, _

_logger = logging.getLogger(__name__)


class SaleOrder(models.Model):
    _inherit = "sale.order"


    @api.onchange('invoice_status')
    def _onchange_invoice_status(self):
        _logger.warning("Invoice status: %s" % self.invoice_status)

Solution

  • The @api.onchange() decorator in Odoo is designed to trigger a method when the value of a field is changed in the user interface. When you change the field value through the UI, the method decorated with @api.onchange() will be invoked. However, if the field value is changed programmatically, such as through code, the @api.onchange() method won't be triggered unless you explicitly call it within your code.

    In your case the first change is done through UI and the second case the sale order state is modified through code.