From 65baadeb759f6bda82ebd781aaa9862141cdd31d Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Thu, 20 Jan 2022 16:21:24 +0000 Subject: [PATCH] [IMP] core: do not try to translate selection values for nothing When we are only trying to know accepted raw values, there is no need to fetch the translations for the description part of the selection tuples. This performance problem was first observed on res.config.settings record creation, where the values are pruned to avoid writes and invalidation for unmodified related values (cf create override in base/models/res_config.py & 5a4e7b711ec1964fcd914ed2ff5f8f1e92db6cef). After more analysis, the 'problem' also impacts record updates on any related field (readonly or not). * Res.config.settings performance gains: In a database with test_main_flows (& its diverse dependencies), this reduces by ~50% the number of queries done when triggering the creation of res.config.settings record through the interface (aka with all the related updatable fields receiving their current value as create value). For the creation of 150 res.config.settings records, with full cache invalidation between each record creation, we notice a gain of: * 11853 to 6303 total queries * 11.2s to 7.5s total creation time Part-of: odoo/odoo#83104 --- odoo/addons/test_new_api/tests/test_new_fields.py | 4 ++-- odoo/fields.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index e76f8c18303..5924e092fc6 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -3164,14 +3164,14 @@ class TestSelectionUpdates(common.TransactionCase): related_record = self.env[self.MODEL_BASE].create({'my_selection': 'foo'}) with self.assertQueryCount(2): # defaults (readonly related field), INSERT record = self.env[self.MODEL_RELATED].create({'selection_id': related_record.id}) - with self.assertQueryCount(4): + with self.assertQueryCount(0): record.related_selection = 'bar' def test_selection_related(self): related_record = self.env[self.MODEL_BASE].create({'my_selection': 'foo'}) with self.assertQueryCount(2): # defaults (related field), INSERT record = self.env[self.MODEL_RELATED_UPDATE].create({'selection_id': related_record.id}) - with self.assertQueryCount(7): + with self.assertQueryCount(3): record.related_selection = 'bar' diff --git a/odoo/fields.py b/odoo/fields.py index d3fd75ad055..4ce963bcef6 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -2589,7 +2589,8 @@ class Selection(Field): if isinstance(selection, str): selection = getattr(env[self.model_name], selection)() elif callable(selection): - selection = selection(env[self.model_name]) + model = env[self.model_name].with_context(lang=None) + selection = selection(model) return [value for value, _ in selection] def convert_to_column(self, value, record, values=None, validate=True):