[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) <dle@odoo.com>
Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>
This commit is contained in:
Denis Ledoux
2023-12-06 20:39:42 +00:00
parent b389016cd3
commit 6e8e3e6333
2 changed files with 16 additions and 4 deletions
+15 -3
View File
@@ -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)
+1 -1
View File
@@ -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_'"
)