From 2f93046cf7efb91a70d3386647f7848e1aabc7e4 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 5 Feb 2021 10:32:49 +0000 Subject: [PATCH] [FIX] core: writing on one2many field on new record When using command "6" (SET), the one2many field's inverse was not assigned on the lines. closes odoo/odoo#65675 X-original-commit: ba733a9ac73dcf978ed7eaab7681781b2d5a9bfc Signed-off-by: Raphael Collet (rco) --- odoo/addons/test_new_api/tests/test_new_fields.py | 7 +++++++ odoo/fields.py | 11 +++++++---- 2 files changed, 14 insertions(+), 4 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 6ed794b6f96..3535a999608 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -1693,6 +1693,13 @@ class TestFields(TransactionCaseWithUserDemo): self.assertFalse(new_move.line_ids.id) self.assertEqual(new_move.line_ids.quantity, 2) + # assign line to new move without origin + new_move = move.new() + new_move.line_ids = line + self.assertFalse(new_move.line_ids.id) + self.assertEqual(new_move.line_ids._origin, line) + self.assertEqual(new_move.line_ids.move_id, new_move) + @mute_logger('odoo.addons.base.models.ir_model') def test_41_new_related(self): """ test the behavior of related fields starting on new records. """ diff --git a/odoo/fields.py b/odoo/fields.py index 01de4ad49eb..012a1a09bbf 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -3428,11 +3428,14 @@ class One2many(_RelationalMulti): browse([command[1]])[inverse] = False elif command[0] == Command.LINK: browse([command[1]])[inverse] = recs[-1] - elif command[0] in (Command.CLEAR, Command.SET): + elif command[0] == Command.CLEAR: + cache.update(recs, self, itertools.repeat(())) + elif command[0] == Command.SET: # assign the given lines to the last record only - cache.update(recs, self, [()] * len(recs)) - lines = comodel.browse(command[2] if command[0] == Command.SET else []) - cache.set(recs[-1], self, lines._ids) + cache.update(recs, self, itertools.repeat(())) + last, lines = recs[-1], browse(command[2]) + cache.set(last, self, lines._ids) + cache.update(lines, inverse_field, itertools.repeat(last.id)) else: def link(record, lines):