From f047425b82457f4da9b1cc0b560bff40ea5060be Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Mon, 23 Sep 2019 12:40:22 +0000 Subject: [PATCH] [IMP] models, fields: initialize cache to None for related fields This makes sense because in `create` the "previous" value of a field is obviously non-existent. This avoids having to fetch pre-existing attachments when they are known to be inexistent, which optimizes stored related binary that are computed post-insert. --- odoo/fields.py | 16 +++++++++++----- odoo/models.py | 2 ++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/odoo/fields.py b/odoo/fields.py index 7670830d770..1bb934cd70c 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -1948,15 +1948,21 @@ class Binary(Field): records = cache.get_records_different_from(records, self, cache_value) if not records: return records + if self.store: + # determine records that are known to be not null + not_null = cache.get_records_different_from(records, self, None) + cache.update(records, self, [cache_value] * len(records)) # retrieve the attachments that store the values, and adapt them if self.store: - atts = records.env['ir.attachment'].sudo().search([ - ('res_model', '=', self.model_name), - ('res_field', '=', self.name), - ('res_id', 'in', records.ids), - ]) + atts = records.env['ir.attachment'].sudo() + if not_null: + atts = atts.search([ + ('res_model', '=', self.model_name), + ('res_field', '=', self.name), + ('res_id', 'in', records.ids), + ]) if value: # update the existing attachments atts.write({'datas': value}) diff --git a/odoo/models.py b/odoo/models.py index 66fae078498..96faaa21289 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -3726,6 +3726,8 @@ Record ids: %(records)s for field in self._fields.values(): if field.type in ('one2many', 'many2many'): self.env.cache.set(record, field, ()) + elif field.related and not field.column_type: + self.env.cache.set(record, field, field.convert_to_cache(None, record)) # DLE P123: `test_adv_activity`, `test_message_assignation_inbox`, `test_message_log`, `test_create_mail_simple`, ... # Set `mail.message.parent_id` to False in cache so it doesn't do the useless SELECT when computing the modified of `child_ids` # in other words, if `parent_id` is not set, no other message `child_ids` are impacted.