Skip to content

OD115 dotted-onchange-constrains

@api.onchange or @api.constrains names a dotted path, which Odoo ignores.

Category Severity Series Needs the index
Correctness Warning all no

What it reports

A string given to @api.onchange or @api.constrains that contains a dot, such as partner_id.country_id.

Why it matters

Both decorators take names of fields of the model itself. Odoo runs an onchange method when the form changes one of the fields it names, and a constraint when a write or a create sets one of them, so a dotted name matches nothing: the method never runs for that name, and Odoo logs a warning the first time it collects the model's onchange or constraint methods, such as when a form opens or a record is created or written. Name the field of the model the path starts from, partner_id: the method then runs when the form links another partner, not when the partner itself changes.

@api.depends takes dotted paths, so it is not reported. Only literal strings are read; a function given to @api.constrains is not.

Example

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

    @api.onchange("partner_id.country_id")
    def _onchange_partner_country(self):
        self.note = self.partner_id.country_id.name
models/sale_order.py
class SaleOrder(models.Model):
    _inherit = "sale.order"

    @api.onchange("partner_id")
    def _onchange_partner_country(self):
        self.note = self.partner_id.country_id.name

Suppressing

@api.onchange("partner_id.country_id")  # odooscan: ignore[OD115]

See suppressing a finding.