From 5dc10a92e16f7655b8a72ec9bbd314fc752be6aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 22 Sep 2024 12:42:42 +0200 Subject: [PATCH] Do not update multivalue record if the value is not modified When a ValueMixin is used for many MultiValue fields, they are created with default values when the first MultiValue is set. But often the other MultiValue fields are set with the default value so by not forcing the change, we avoid a unnecessary UPDATE query. --- trytond/trytond/model/multivalue.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/trytond/trytond/model/multivalue.py b/trytond/trytond/model/multivalue.py index 216b25db6d..7f0b3172e9 100644 --- a/trytond/trytond/model/multivalue.py +++ b/trytond/trytond/model/multivalue.py @@ -84,7 +84,14 @@ class MultiValueMixin(object): if not values: values = [self.multivalue_record(name, **pattern)] for record in values: - setattr(record, name, value) + current_value = getattr(record, name, None) + if isinstance(current_value, Model): + current_value = current_value.id + elif isinstance(current_value, (list, tuple)): + current_value = [ + r.id if isinstance(r, Model) else r for r in current_value] + if current_value != value: + setattr(record, name, value) if save: Value.save(values) else: