[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
This commit is contained in:
committed by
mreficent
parent
b08aa26d8f
commit
186b1c09cc
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user