From b6b26cc8124ecfce33ef7b604fc4d13a5dbf5808 Mon Sep 17 00:00:00 2001 From: Martin Geubelle Date: Wed, 31 Aug 2016 14:19:37 +0200 Subject: [PATCH] [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 --- openerp/addons/base/ir/ir_model.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/openerp/addons/base/ir/ir_model.py b/openerp/addons/base/ir/ir_model.py index e951ca290d7..f52184a5b91 100644 --- a/openerp/addons/base/ir/ir_model.py +++ b/openerp/addons/base/ir/ir_model.py @@ -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):