OD207 related-type-mismatch¶
A related field has another type than the field it relates to.
| Category | Severity | Series | Needs the index |
|---|---|---|---|
| Correctness | Error | all | yes |
What it reports¶
A field with related whose declared type differs from the type of the field its path ends on, as
the module's dependencies define that field. A module that Odoo always installs with base, such as
web, counts as in depends. Image counts as Binary, as it does for Odoo.
Why it matters¶
Odoo compares the two types when it sets up the registry and raises TypeError when they differ,
so the module cannot be installed and a database that already has it fails to start. A related
field holds its target's value as it is and never converts it, so the two types must match.
The rule checks only a path that resolves; one that does not is reported by
OD203. A path through a model no indexed module defines, ending on a field every model
has, such as display_name or create_date, or ending on a field an ir.model.fields record adds,
as OD304 describes, gives no type to compare. Related fields of an abstract model are
not checked, because their paths name fields of the models that inherit it.
Example¶
class SaleOrder(models.Model):
_inherit = "sale.order"
team_leader = fields.Char(related="team_id.user_id")
total = fields.Float(related="amount_total")
team_id.user_id is a Many2one and amount_total is a Monetary, so neither type matches.
class SaleOrder(models.Model):
_inherit = "sale.order"
team_leader_id = fields.Many2one(related="team_id.user_id")
total = fields.Monetary(related="amount_total")
A related relational field may leave out its comodel: Odoo takes it from the target field.