[IMP] base: allow quick create on ir.model

This option implies 3 changes:
 * a default char field x_name on the new model
 * be able to infer the `model` of the ir.model in name_create
   from its `name`
 * avoid deleting the registry when unlinking *nothing* ; the default
   field_id or ir.model ([(0, 0, ...)]) implies an unlink ([(5,), (0, 0)])
   that delete the current registry

Courtesy of @rco-odoo
This commit is contained in:
Martin Geubelle
2016-09-01 14:06:22 +02:00
parent 9b269cb0dd
commit b6b26cc812
+14 -1
View File
@@ -42,7 +42,8 @@ class IrModel(models.Model):
name = fields.Char(string='Model Description', translate=True, required=True)
model = fields.Char(default='x_', required=True, index=True)
info = fields.Text(string='Information')
field_id = fields.One2many('ir.model.fields', 'model_id', string='Fields', required=True, copy=True)
field_id = fields.One2many('ir.model.fields', 'model_id', string='Fields', required=True, copy=True,
default=lambda self: [(0, 0, {'name': 'x_name', 'field_description': 'Name', 'ttype': 'char'})])
inherited_model_ids = fields.Many2many('ir.model', compute='_inherited_models', string="Inherited models",
help="The list of models that extends the current model.")
state = fields.Selection([('manual', 'Custom Object'), ('base', 'Base Object')], string='Type', default='manual', readonly=True)
@@ -153,6 +154,15 @@ class IrModel(models.Model):
self.pool.signal_registry_change()
return res
@api.model
def name_create(self, name):
""" Infer the model from the name. E.g.: 'My New Model' should become 'x_my_new_model'. """
vals = {
'name': name,
'model': 'x_' + '_'.join(name.lower().split(' ')),
}
return self.create(vals).name_get()[0]
@api.model
def _instanciate(self, model_data):
""" Instanciate a model class for the custom model given by parameters ``model_data``. """
@@ -396,6 +406,9 @@ class IrModelFields(models.Model):
@api.multi
def unlink(self):
if not self:
return True
# Prevent manual deletion of module columns
if not self._context.get(MODULE_UNINSTALL_FLAG) and \
any(field.state != 'manual' for field in self):