From e74fe80df94e59cfe50a6aebc71fbc32ab3ef456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 14 Apr 2023 13:09:23 +0200 Subject: [PATCH] Convert instances in RPC call result for on_change_with on field Since d142b33fa3ab0fe5af25957f90ee77fe7eee51b3 the on_change_with method can return instances. Closes #12217 --- trytond/trytond/model/fields/field.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/trytond/trytond/model/fields/field.py b/trytond/trytond/model/fields/field.py index 8f1c5bdead..f846e4c4aa 100644 --- a/trytond/trytond/model/fields/field.py +++ b/trytond/trytond/model/fields/field.py @@ -1,7 +1,7 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. import warnings -from functools import wraps +from functools import partial, wraps import sql from sql import ( @@ -195,6 +195,20 @@ def on_change_result(record): return record._changed_values +def on_change_with_result(field, value): + from ..modelstorage import ModelStorage + if field._type in {'many2one', 'one2one', 'reference'}: + if isinstance(value, ModelStorage): + if field._type == 'reference': + value = str(value) + else: + value = value.id + elif field._type in {'one2many', 'many2many'}: + if isinstance(value, (list, tuple)): + value = [int(r) for r in value] + return value + + def domain_method(func): @wraps(func) def wrapper(self, domain, tables, Model): @@ -455,7 +469,7 @@ class Field(object): def set_rpc(self, model): for attribute, result in ( ('on_change', on_change_result), - ('on_change_with', None), + ('on_change_with', partial(on_change_with_result, self)), ): if not getattr(self, attribute): continue