[FIX] fields: Better error for invalid @depends

Create a model as follow:

    class FooModel(models.Model):
        _name = 'foo'
        bar = fields.Char(compute="compute_bar")

        @depends('bazz')  # wrong dependency, bazz does not exist
        def compute_bar(self):
            for rec in self:
                rec.bar = "Babar raconte des beaux bobards."

Uppon -u foo_module, a ValueError is rose for the invalid dependency
with the following message:

> Field foo.bar cannot find dependency bazz on model foo.

We had feedback the error message was not very clear to some users,
the new error message is:

> Wrong @depends on 'compute_bar' (compute method of field foo.bar).
> Dependency field 'bazz' not found in model foo.

closes odoo/odoo#62328

Task: 2366612
Signed-off-by: Julien Castiaux <Julien00859@users.noreply.github.com>
This commit is contained in:
Julien Castiaux
2020-11-26 09:17:30 +00:00
parent 4b28f1162a
commit a152c536b7
+4 -2
View File
@@ -637,8 +637,10 @@ class Field(MetaField('DummyField', (object,), {})):
try:
field = Model._fields[fname]
except KeyError:
msg = "Field %s cannot find dependency %r on model %r."
raise ValueError(msg % (self, fname, model_name))
raise ValueError(
f"Wrong @depends on '{self.compute}' (compute method of field {self}). "
f"Dependency field '{fname}' not found in model {model_name}."
)
if field is self and index:
self.recursive = True