Skip to content

OD117 create-not-batched

create() is decorated with @api.model, so Odoo calls it once per record.

Category Severity Series Needs the index
Deprecation Warning saas~15.2 and later, before saas~18.2 no

What it reports

A create method of a model class decorated with @api.model. The finding is at the decorator.

Why it matters

Odoo wraps a create decorated with @api.model so that it takes one dict, and when it loads the module, says that the model does not create in batch: from saas~15.2 to saas~17.4 it logs a warning, and from 18.0 it issues a DeprecationWarning. Given a list of dicts, as copy() and imports pass, the wrapper calls the method once per dict, so each record goes through every override, and through the ORM's inserts and recomputations, on its own. Decorate the method with @api.model_create_multi, take the list, and loop over it.

Before saas~15.2, Odoo wraps the method without saying so, and the rule does not run.

From saas~18.2, so in 19.0, @api.model on create is @api.model_create_multi itself, so the method receives the list whatever it expects: OD118 reports one that still reads its argument as a dict.

Example

models/res_partner.py
class ResPartner(models.Model):
    _inherit = "res.partner"

    @api.model
    def create(self, vals):
        vals.setdefault("comment", "Imported")
        return super().create(vals)
models/res_partner.py
class ResPartner(models.Model):
    _inherit = "res.partner"

    @api.model_create_multi
    def create(self, vals_list):
        for vals in vals_list:
            vals.setdefault("comment", "Imported")
        return super().create(vals_list)

Suppressing

@api.model  # odooscan: ignore[OD117]

See suppressing a finding.