From 1d5fa4a522402034fb07111527ddea4fecdcbd3c Mon Sep 17 00:00:00 2001 From: "Laurent Mignon (ACSONE)" Date: Tue, 10 Sep 2019 10:00:02 +0200 Subject: [PATCH] [FIX] base_custom_info: Check contrain on model into template before the write We must avoid to rely on the order in which computed fields (including related fields) and constrains methods are applied. Due to a recent change into the ORM, the contrains on the model_id into CustomInfoTemplate is now called AFTER the recompute of the related model field into property_ids.info_value_ids As side effect, when the constrains is called, the model on the info value is already updated with the new value and we no more know the old value.... --- .../models/custom_info_template.py | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/base_custom_info/models/custom_info_template.py b/base_custom_info/models/custom_info_template.py index aeb22b46a..d392a4a00 100644 --- a/base_custom_info/models/custom_info_template.py +++ b/base_custom_info/models/custom_info_template.py @@ -53,18 +53,18 @@ class CustomInfoTemplate(models.Model): self._inverse_model() @api.multi - @api.constrains("model_id") - def _check_model(self): - """Avoid error when updating base module and a submodule extends a - model that falls out of this one's dependency graph. + def _check_model_update_allowed(self, model_id): + """Check if the template's model can be updated. + + Template can be updated only if no property values already exists for + this template """ for record in self: - with self.env.norecompute(): - oldmodels = record.mapped("property_ids.info_value_ids.model") - if oldmodels and record.model not in oldmodels: - raise ValidationError( - _("You cannot change the model because it is in use.") - ) + if (model_id != record.model_id.id + and record.mapped("property_ids.info_value_ids")): + raise ValidationError( + _("You cannot change the model because it is in use.") + ) @api.multi def check_access_rule(self, operation): @@ -74,3 +74,9 @@ class CustomInfoTemplate(models.Model): model.check_access_rights(operation) model.check_access_rule(operation) return super(CustomInfoTemplate, self).check_access_rule(operation) + + @api.multi + def write(self, vals): + if 'model_id' in vals: + self._check_model_update_allowed(vals['model_id']) + return super(CustomInfoTemplate, self).write(vals)