From 021c1a26c55c9f9b9be6269c64d06c350ad1f5fe Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Mon, 2 Mar 2015 14:26:39 +0100 Subject: [PATCH] [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. --- openerp/fields.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/openerp/fields.py b/openerp/fields.py index b78f5b330d2..2688e7e56ca 100644 --- a/openerp/fields.py +++ b/openerp/fields.py @@ -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