Instantiate One2One target when the context of the field

Closes #13819
(grafted from 0b5ee6e071f83054de3a0c518318c4d1bd11d449)

--HG--
branch : 7.0
This commit is contained in:
Cédric Krier
2025-01-14 01:19:00 +01:00
parent c2ddb35142
commit 4c8d371cf6
+9 -5
View File
@@ -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)