Skip to content

Commit

Permalink
[FIX] fields: performance of convert_to_onchange on x2many fields
Browse files Browse the repository at this point in the history
Suppose we convert a x2many field `foo_ids` with a many2one sub-field `bar_id`.
The conversion of the values of `bar_id` generates pairs `(id, name)`.  Before
this patch, the method `name_get` is invoked on every value, one by one.
Rewrite the code to ensure that `name_get()` is invoked on all values at once.
  • Loading branch information
rco-odoo committed Dec 6, 2017
1 parent a9a1895 commit 6da1c3a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions odoo/fields.py
Expand Up @@ -1995,7 +1995,7 @@ def convert_to_read(self, value, record, use_name_get=True):
# access rights, and not the value's access rights.
try:
# performance: value.sudo() prefetches the same records as value
return value.sudo().name_get()[0]
return (value.id, value.sudo().display_name)
except MissingError:
# Should not happen, unless the foreign key is missing.
return False
Expand Down Expand Up @@ -2112,17 +2112,22 @@ def convert_to_onchange(self, value, record, names):
# return the recordset value as a list of commands; the commands may
# give all fields values, the client is responsible for figuring out
# which fields are actually dirty
vals = {record: {} for record in value}
for name, subnames in names.items():
if name == 'id':
continue
field = value._fields[name]
# read all values before converting them (better prefetching)
rec_vals = [(rec, rec[name]) for rec in value]
for rec, val in rec_vals:
vals[rec][name] = field.convert_to_onchange(val, rec, subnames)

result = [(5,)]
for record in value:
vals = {
name: value._fields[name].convert_to_onchange(record[name], record, subnames)
for name, subnames in names.items()
if name != 'id'
}
if not record.id:
result.append((0, record.id.ref or 0, vals))
elif vals:
result.append((1, record.id, vals))
result.append((0, record.id.ref or 0, vals[record]))
elif vals[record]:
result.append((1, record.id, vals[record]))
else:
result.append((4, record.id))
return result
Expand Down

0 comments on commit 6da1c3a

Please sign in to comment.