From ddcabf3acb006866af12db5bd3dcdfee4501335c Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 28 May 2021 18:29:07 +0000 Subject: [PATCH] [FIX] core: origin of main record in onchange When invoking onchange() on a line in a one2many field, the inverse many2one field is given as a dict of values. Those values are the ones of the "container" record in the form view, and they include the "id" of the container record. The method onchange() instantiates the container record as a new record with the given values. Fix the code to set the expected "origin" of that record to the record with the given "id". closes odoo/odoo#71491 X-original-commit: ebfb5d90fd4ff65ee1d2fc73faf8f9eaa18f4521 Signed-off-by: Raphael Collet (rco) --- .../addons/test_new_api/tests/test_new_fields.py | 16 ++++++++++++++++ odoo/fields.py | 5 ++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index 4a82cb355ac..db0110199f7 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -1681,6 +1681,22 @@ class TestFields(TransactionCaseWithUserDemo): self.assertNotEqual(new_disc.participants, disc.participants) self.assertEqual(new_disc.participants._origin, disc.participants) + # provide many2one field as a dict of values; the value is a new record + # with the given 'id' as origin (if given, of course) + new_msg = disc.messages.new({ + 'discussion': {'name': disc.name}, + }) + self.assertTrue(new_msg.discussion) + self.assertFalse(new_msg.discussion.id) + self.assertFalse(new_msg.discussion._origin) + + new_msg = disc.messages.new({ + 'discussion': {'name': disc.name, 'id': disc.id}, + }) + self.assertTrue(new_msg.discussion) + self.assertFalse(new_msg.discussion.id) + self.assertEqual(new_msg.discussion._origin, disc) + # check convert_to_write tag = self.env['test_new_api.multi.tag'].create({'name': 'Foo'}) rec = self.env['test_new_api.multi'].create({ diff --git a/odoo/fields.py b/odoo/fields.py index 664c49e50ab..07c789847ee 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -2748,7 +2748,10 @@ class Many2one(_Relational): # value is either a pair (id, name), or a tuple of ids id_ = value[0] if value else None elif isinstance(value, dict): - id_ = record.env[self.comodel_name].new(value).id + # return a new record (with the given field 'id' as origin) + comodel = record.env[self.comodel_name] + origin = comodel.browse(value.get('id')) + id_ = comodel.new(value, origin=origin).id else: id_ = None