From 4c8d371cf68df235928f8fec7311fe47dd60c3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 14 Jan 2025 01:19:00 +0100 Subject: [PATCH] Instantiate One2One target when the context of the field Closes #13819 (grafted from 0b5ee6e071f83054de3a0c518318c4d1bd11d449) --HG-- branch : 7.0 --- trytond/trytond/model/fields/one2one.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/trytond/trytond/model/fields/one2one.py b/trytond/trytond/model/fields/one2one.py index 9ae9dd36e1..2d4f197270 100644 --- a/trytond/trytond/model/fields/one2one.py +++ b/trytond/trytond/model/fields/one2one.py @@ -1,9 +1,10 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. -from trytond.model.fields.field import Field +from trytond.model.fields.field import Field, instantiate_context from trytond.model.fields.many2many import Many2Many from trytond.pool import Pool +from trytond.transaction import Transaction class One2One(Many2Many): @@ -56,9 +57,12 @@ class One2One(Many2Many): def __set__(self, inst, value): Target = self.get_target() - if isinstance(value, dict): - value = Target(**value) - elif isinstance(value, int): - value = Target(value) + if isinstance(value, (dict, int)): + ctx = instantiate_context(self, inst) + with Transaction().set_context(ctx): + if isinstance(value, dict): + value = Target(**value) + elif isinstance(value, int): + value = Target(value) assert isinstance(value, (Target, type(None))) Field.__set__(self, inst, value)