summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphael Collet <rco@odoo.com>2021-05-28 18:29:07 +0000
committerRaphael Collet <rco@odoo.com>2021-05-31 09:48:22 +0000
commit21003506c061e59ec89d0e0450fbcd1d3e2da825 (patch)
tree7c53d2fb8dc80288dea0d171a7a1073206c79c36
parenta11662e525aad9a843acf54b717f3df18d8c2cab (diff)
[FIX] core: origin of main record in onchangeHEAD14.0
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#71474 X-original-commit: ebfb5d90fd4ff65ee1d2fc73faf8f9eaa18f4521 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
-rw-r--r--odoo/addons/test_new_api/tests/test_new_fields.py16
-rw-r--r--odoo/fields.py5
2 files changed, 20 insertions, 1 deletions
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 b3a83605d19..b10f10d3609 100644
--- a/odoo/addons/test_new_api/tests/test_new_fields.py
+++ b/odoo/addons/test_new_api/tests/test_new_fields.py
@@ -1678,6 +1678,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 64f2cb01edd..e1bf6331df8 100644
--- a/odoo/fields.py
+++ b/odoo/fields.py
@@ -2650,7 +2650,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