From 74ff54227819cef4cf951d9ab3a1fe324decc43b Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Tue, 19 Oct 2021 13:44:14 +0000 Subject: [PATCH] [IMP] base: avoid setup_models() when creating custom selection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There exists an optimization when creating fields to avoid calling `setup_models()` for fields when creating a custom model, since the latter already calls `setup_models()`. We add the same optimization for field selections, so that creating a custom field with selections only calls `setup_models()` once. Note that both optimizations are combined when creating custom models with selection fields: `setup_models()` will be called once for all. closes odoo/odoo#78514 Related: odoo/enterprise#21746 Signed-off-by: Xavier Dollé (xdo) --- odoo/addons/base/models/ir_model.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/odoo/addons/base/models/ir_model.py b/odoo/addons/base/models/ir_model.py index 8b08d81702f..87987531f8c 100644 --- a/odoo/addons/base/models/ir_model.py +++ b/odoo/addons/base/models/ir_model.py @@ -1319,16 +1319,22 @@ class IrModelSelection(models.Model): @api.model_create_multi def create(self, vals_list): field_ids = {vals['field_id'] for vals in vals_list} + field_names = set() for field in self.env['ir.model.fields'].browse(field_ids): + field_names.add((field.model, field.name)) if field.state != 'manual': raise UserError(_('Properties of base fields cannot be altered in this manner! ' 'Please modify them through Python code, ' 'preferably through a custom addon!')) recs = super().create(vals_list) - # setup models; this re-initializes model in registry - self.flush() - self.pool.setup_models(self._cr) + if any( + model in self.pool and name in self.pool[model]._fields + for model, name in field_names + ): + # setup models; this re-initializes model in registry + self.flush() + self.pool.setup_models(self._cr) return recs