Skip to content

Check to avoid harcoding methods in field definition #126

@luisg123v

Description

@luisg123v

Cases like:

name = fields.Char(
    compute=_compute_name,
    default=_default_name,
)

Should be avoided because:

  • Methods are harcoded, which means they can't be inherited, because set methods are from that specific class, instead of model methods.
  • We are forced to define such methods above field definition, which is against our guidelines, which specifies fields should go first, then methods.

Instead, the following should be done:

name = fields.Char(
    compute="_compute_name",
    default=lambda self: self._default_name(),
)

i.e. passing computed method as a string and default method as a lambda.

Attributes to consider for both cases

The following attributes support receiving a string:

  • compute
  • inverse
  • search
  • selection

(See the odoo.fields.determine method for more info and all the places where it is called)

The following attributes do not support receiving a string, hence they require passing a lambda:

  • default
  • domain
  • translate
  • ondelete (dictionary values, not the dictionary itself)

(See all appearances of the callable() function in odoo.fields)

Discouraging cases like the above will prevent issues like odoo/odoo#185419.

CC @moylop260 @teposteaj

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions