From 186b1c09ccd8092c8ec5c43ae1e70fa91caeb5e0 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 26 Jun 2018 13:20:23 +0000 Subject: [PATCH] [FIX] base: avoid prefetching all the fields when getting context When getting the context of res_users, all the fields are read but if the schema is being modified and the modifications are not yet commited in the database, this leads to a bad query. With this commit, a read is used to fetch only the needed fields. Thanks to @RCO for finding this issue that only occurs in specific planetary alignment. closes odoo/odoo#27263 --- odoo/addons/base/res/res_users.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/odoo/addons/base/res/res_users.py b/odoo/addons/base/res/res_users.py index 6b9be0b3e6e..30eba4f3c0d 100644 --- a/odoo/addons/base/res/res_users.py +++ b/odoo/addons/base/res/res_users.py @@ -423,20 +423,19 @@ class Users(models.Model): @tools.ormcache('self._uid') def context_get(self): user = self.env.user - result = {} - for k in self._fields: - if k.startswith('context_'): - context_key = k[8:] - elif k in ['lang', 'tz']: - context_key = k - else: - context_key = False - if context_key: - res = getattr(user, k) or False - if isinstance(res, models.BaseModel): - res = res.id - result[context_key] = res or False - return result + # determine field names to read + name_to_key = { + name: name[8:] if name.startswith('context_') else name + for name in self._fields + if name.startswith('context_') or name in ('lang', 'tz') + } + # use read() to not read other fields: this must work while modifying + # the schema of models res.users or res.partner + values = user.read(list(name_to_key), load=False)[0] + return { + key: values[name] + for name, key in name_to_key.items() + } @api.model @api.returns('ir.actions.act_window', lambda record: record.id)