[FIX] base: optimize the reading of many2one property fields

back port of commit 9820191d33f773ae21ca405da0e1226b6657ef5b
(made for v11)

project : Performance Issues
task : Geostaff : Success Pack 5 (100h) (opw-1912303)

closes odoo/odoo#29689
This commit is contained in:
Moens Alexandre
2019-01-29 13:12:49 +00:00
parent 917127ff70
commit 877fe07f74
+24 -5
View File
@@ -160,11 +160,30 @@ class Property(models.Model):
# note: order by 'company_id asc' will return non-null values first
props = self.search(domain, order='company_id asc')
result = {}
for prop in props:
# for a given res_id, take the first property only
id = refs.pop(prop.res_id, None)
if id is not None:
result[id] = prop.get_by_record()
field = self.env[model]._fields[name]
if field.type == 'many2one':
# optimization for many2one fields
Comodel = self.env[field.comodel_name]
co_ids = set()
for prop in props:
# for a given res_id, take the first property only
id = refs.pop(prop.res_id, None)
if id is not None:
val = prop.value_reference
if val:
val = int(val.split(',')[1])
co_ids.add(val)
result[id] = val
# check for existence in batch, and update result accordingly
existing = {rec.id: rec for rec in Comodel.browse(co_ids).exists()}
result = {id: existing.get(val, Comodel) for id, val in result.items()}
else:
for prop in props:
# for a given res_id, take the first property only
id = refs.pop(prop.res_id, None)
if id is not None:
result[id] = prop.get_by_record()
# set the default value to the ids that are not in result
default_value = result.pop(False, False)