[FIX] core: update of one2many field on new record with origin

An onchange method can remove an existing line from a one2many field of
a new record with command 2, such as:

    move.line_ids = [(2, <NewId origin=42>), ...]

The processing of the command 2 above incorrectly tries to remove the
new line from the origin record, instead of the new record.

closes odoo/odoo#54545

X-original-commit: 7d138a445acbd9f2e122d9c30ca81877bae6a245
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Raphael Collet
2020-07-16 08:16:16 +00:00
parent 800fa0812a
commit a21e8afda4
2 changed files with 21 additions and 1 deletions
@@ -1473,6 +1473,22 @@ class TestFields(TransactionCaseWithUserDemo):
self.assertEqual(new_move.quantity, 3)
self.assertEqual(move.quantity, 2)
def test_41_new_one2many(self):
""" Check command on one2many field on new record. """
move = self.env['test_new_api.move'].create({})
line = self.env['test_new_api.move_line'].create({'move_id': move.id, 'quantity': 1})
move.flush()
new_move = move.new(origin=move)
new_line = line.new(origin=line)
self.assertEqual(new_move.line_ids, new_line)
# drop line, and create a new one
new_move.line_ids = [(2, new_line.id), (0, 0, {'quantity': 2})]
self.assertEqual(len(new_move.line_ids), 1)
self.assertFalse(new_move.line_ids.id)
self.assertEqual(new_move.line_ids.quantity, 2)
@mute_logger('odoo.addons.base.models.ir_model')
def test_41_new_related(self):
""" test the behavior of related fields starting on new records. """
+5 -1
View File
@@ -2638,9 +2638,13 @@ class Many2one(_Relational):
""" Remove `records` from the cached values of the inverse fields of `self`. """
cache = records.env.cache
record_ids = set(records._ids)
# align(id) returns a NewId if records are new, a real id otherwise
align = (lambda id_: id_) if all(record_ids) else (lambda id_: id_ and NewId(id_))
for invf in records._field_inverses[self]:
corecords = records.env[self.comodel_name].browse(
id_ for id_ in cache.get_values(records, self)
align(id_) for id_ in cache.get_values(records, self)
)
for corecord in corecords:
ids0 = cache.get(corecord, invf, None)