From 6e8e3e6333c70bf4e88ecdc25cc3b629298bfbe8 Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Tue, 5 Dec 2023 10:16:25 +0100 Subject: [PATCH] [IMP] core: allow to override model/field manual name rule At Odoo, we want custom models and fields to start with `x_`. However, other developers and companies might want to customize that behavior and be able to change the rule. This revision targets to factorize the rule `startswith('x_')` in a dedicated method on `ir.model` and `ir.model.fields` so they can be overridden to allow the customization of custom model and fields name in custom modules. closes odoo/odoo#145270 X-original-commit: edf979c7fddbf05b915393923ed57680b9ba5399 Related: odoo/enterprise#52263 Signed-off-by: Denis Ledoux (dle) Signed-off-by: Christophe Simonis (chs) --- odoo/addons/base/models/ir_model.py | 18 +++++++++++++++--- odoo/models.py | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/odoo/addons/base/models/ir_model.py b/odoo/addons/base/models/ir_model.py index dfb0238e327..2f4ffb39f31 100644 --- a/odoo/addons/base/models/ir_model.py +++ b/odoo/addons/base/models/ir_model.py @@ -244,8 +244,7 @@ class IrModel(models.Model): def _check_model_name(self): for model in self: if model.state == 'manual': - if not model.model.startswith('x_'): - raise ValidationError(_("The model name must start with 'x_'.")) + self._check_manual_name(model.model) if not models.check_object_name(model.model): raise ValidationError(_("The model name can only contain lowercase characters, digits, underscores and dots.")) @@ -443,6 +442,15 @@ class IrModel(models.Model): return CustomModel + @api.model + def _is_manual_name(self, name): + return name.startswith('x_') + + @api.model + def _check_manual_name(self, name): + if not self._is_manual_name(name): + raise ValidationError(_("The model name must start with 'x_'.")) + def _add_manual_models(self): """ Add extra models to the registry. """ # clean up registry first @@ -1255,7 +1263,7 @@ class IrModelFields(models.Model): elif field_data['ttype'] == 'monetary': # be sure that custom monetary field are always instanciated if not self.pool.loaded and \ - not (field_data['currency_field'] and field_data['currency_field'].startswith('x_')): + not (field_data['currency_field'] and self._is_manual_name(field_data['currency_field'])): return attrs['currency_field'] = field_data['currency_field'] # add compute function if given @@ -1269,6 +1277,10 @@ class IrModelFields(models.Model): if attrs: return fields.Field.by_type[field_data['ttype']](**attrs) + @api.model + def _is_manual_name(self, name): + return name.startswith('x_') + def _add_manual_fields(self, model): """ Add extra fields on model. """ fields_data = self._get_manual_field_data(model._name) diff --git a/odoo/models.py b/odoo/models.py index 24c38cea41a..944028f929b 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -645,7 +645,7 @@ class BaseModel(metaclass=MetaModel): isinstance(getattr(model, name, None), fields.Field) for model in [cls] + [self.env.registry[inherit] for inherit in cls._inherits] ) - if not (is_class_field or name.startswith('x_')): + if not (is_class_field or self.env['ir.model.fields']._is_manual_name(name)): raise ValidationError( f"The field `{name}` is not defined in the `{cls._name}` Python class and does not start with 'x_'" )