Skip to content

OD104 default-evaluated-at-import

A field default is called once at import instead of on each record.

Category Severity Series Needs the index
Correctness Error all no

What it reports

A field whose default is the result of calling a function named today, now or context_today, such as fields.Date.today(), fields.Datetime.now() or datetime.now().

Why it matters

Python evaluates the arguments of a field when it reads the class, which happens once, when the server imports the module. The default is then a fixed date: every record created afterwards gets the day the server last started, and nothing fails to make it visible. Odoo calls a default that is a function on each new record, so pass the function without calling it. fields.Date.context_today gives the date in the user's timezone, fields.Date.today the date in UTC.

Only calls to those three names are reported, wherever they come from. A lambda or a function passed as the default runs per record and is left alone.

Example

models/library_loan.py
class LibraryLoan(models.Model):
    _name = "library.loan"
    _description = "Library loan"

    loan_date = fields.Date(default=fields.Date.today())

fields.Date.today() runs at import, so every loan gets the date of the last server restart.

models/library_loan.py
class LibraryLoan(models.Model):
    _name = "library.loan"
    _description = "Library loan"

    loan_date = fields.Date(default=fields.Date.context_today)

Suppressing

epoch = fields.Date(default=fields.Date.today())  # odooscan: ignore[OD104]

See suppressing a finding.