From 5771772b610cbb64e30a1baa23980c3867248fd7 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Wed, 26 Feb 2020 12:43:03 +0000 Subject: [PATCH] [FIX] core: do not force recomputation of fields on new records The method `recompute()` should not recompute fields on new records. This is both a speedup (those recomputations are not necessary), and fixes an error following an `onchange()` (a field recomputed in an environment with the wrong context.) OPW 2184998 closes odoo/odoo#47545 X-original-commit: fa852ba1c5707b71469c410063f338eef261ab2b Signed-off-by: Raphael Collet (rco) --- odoo/models.py | 8 +++++--- odoo/tests/common.py | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/odoo/models.py b/odoo/models.py index 9ca62161f2b..998e150b962 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -5849,6 +5849,9 @@ Record ids: %(records)s if not recs: return if field.compute and field.store: + # do not force recomputation on new records; those will be + # recomputed by accessing the field on the records + recs = recs.filtered('id') try: recs.mapped(field.name) except MissingError: @@ -5865,9 +5868,8 @@ Record ids: %(records)s if fnames is None: # recompute everything - fields_to_compute = self.env.fields_to_compute() - while fields_to_compute: - process(next(iter(fields_to_compute))) + for field in list(self.env.fields_to_compute()): + process(field) else: fields = [self._fields[fname] for fname in fnames] diff --git a/odoo/tests/common.py b/odoo/tests/common.py index 2172bab1dc9..a86f0cc8bcf 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -1813,7 +1813,7 @@ class Form(object): ) self._changed.clear() self._model.flush() - self._model.invalidate_cache() + self._model.env.clear() # discard cache and pending recomputations return r def _values_to_save(self, all_fields=False): @@ -1901,7 +1901,7 @@ class Form(object): record = self._model.browse(self._values.get('id')) result = record.onchange(self._onchange_values(), fields, spec) self._model.flush() - self._model.invalidate_cache() + self._model.env.clear() # discard cache and pending recomputations if result.get('warning'): _logger.getChild('onchange').warning("%(title)s %(message)s" % result.get('warning')) values = result.get('value', {})