[IMP] fields: improve performance when reading many2one fields on records

Improve the performance of `name_get()` on many2one field values by making sure
that the records on which `name_get` is invoked are prefetched in cache.  This
is not automatic, because `name_get` is invoked on records in another
environment (sudo mode): the prefetching in the original environment should be
reproduced in the other environment.
This commit is contained in:
Raphael Collet 2015-03-02 14:26:39 +01:00
parent dc41a9b645
commit 021c1a26c5
1 changed files with 5 additions and 1 deletions

View File

@ -1482,7 +1482,11 @@ class Many2one(_Relational):
# many2one field value (id and name) depends on the current record's
# access rights, and not the value's access rights.
try:
return value.sudo().name_get()[0]
value_sudo = value.sudo()
# performance trick: make sure that all records of the same
# model as value in value.env will be prefetched in value_sudo.env
value_sudo.env.prefetch[value._name].update(value.env.prefetch[value._name])
return value_sudo.name_get()[0]
except MissingError:
# Should not happen, unless the foreign key is missing.
return False