[FIX] base: fix validation for co-model field in ir_field view
The co-model field in `IrField` view is accepting Related Model name even if it is invalid. And replaces the invalid model name with `_unknown`. This causes KeyError `display_name` when trying to create or read a record. To fix that we are adding Validations for the Related Model field when it gets updated from UI side. sentry-3933471439 closes odoo/odoo#118052 X-original-commit: e4f7798fd5a5cccd5d385f535fbc21264cb6948b Signed-off-by: Rémy Voet <ryv@odoo.com>
This commit is contained in:
@@ -22402,6 +22402,13 @@ msgstr ""
|
||||
msgid "Unknown field name '%s' in related field '%s'"
|
||||
msgstr ""
|
||||
|
||||
#. module: base
|
||||
#. odoo-python
|
||||
#: code:addons/base/models/ir_model.py:0
|
||||
#, python-format
|
||||
msgid "Unknown model name '%s' in Related Model"
|
||||
msgstr ""
|
||||
|
||||
#. module: base
|
||||
#. odoo-python
|
||||
#: code:addons/base/models/ir_fields.py:0
|
||||
|
||||
@@ -638,6 +638,19 @@ class IrModelFields(models.Model):
|
||||
self.relation = field.relation
|
||||
self.readonly = True
|
||||
|
||||
@api.onchange('relation')
|
||||
def _onchange_relation(self):
|
||||
try:
|
||||
self._check_relation()
|
||||
except ValidationError as e:
|
||||
return {'warning': {'title': _("Model %s does not exist", self.relation), 'message': e}}
|
||||
|
||||
@api.constrains('relation')
|
||||
def _check_relation(self):
|
||||
for rec in self:
|
||||
if rec.state == 'manual' and rec.relation and not rec.env['ir.model']._get_id(rec.relation):
|
||||
raise ValidationError(_("Unknown model name '%s' in Related Model", rec.relation))
|
||||
|
||||
@api.constrains('depends')
|
||||
def _check_depends(self):
|
||||
""" Check whether all fields in dependencies are valid. """
|
||||
@@ -684,12 +697,7 @@ class IrModelFields(models.Model):
|
||||
def _onchange_ttype(self):
|
||||
if self.ttype == 'many2many' and self.model_id and self.relation:
|
||||
if self.relation not in self.env:
|
||||
return {
|
||||
'warning': {
|
||||
'title': _('Model %s does not exist', self.relation),
|
||||
'message': _('Please specify a valid model for the object relation'),
|
||||
}
|
||||
}
|
||||
return
|
||||
names = self._custom_many2many_names(self.model_id.model, self.relation)
|
||||
self.relation_table, self.column1, self.column2 = names
|
||||
else:
|
||||
|
||||
@@ -563,6 +563,21 @@ class TestCustomFields(common.TransactionCase):
|
||||
for partner in partners:
|
||||
self.assertEqual(partner.x_oh_boy, partner.country_id.code)
|
||||
|
||||
def test_relation_of_a_custom_field(self):
|
||||
""" change the relation model of a custom field """
|
||||
model = self.env['ir.model'].search([('model', '=', self.MODEL)])
|
||||
field = self.env['ir.model.fields'].create({
|
||||
'name': 'x_foo',
|
||||
'model_id': model.id,
|
||||
'field_description': 'x_foo',
|
||||
'ttype': 'many2many',
|
||||
'relation': self.COMODEL,
|
||||
})
|
||||
|
||||
# change the relation
|
||||
with self.assertRaises(ValidationError):
|
||||
field.relation = 'foo'
|
||||
|
||||
def test_selection(self):
|
||||
""" custom selection field """
|
||||
Model = self.env[self.MODEL]
|
||||
|
||||
Reference in New Issue
Block a user