diff options
| author | Raphael Collet <rco@odoo.com> | 2020-07-15 12:35:06 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2020-07-16 08:16:16 +0000 |
| commit | a21e8afda431281a4e0fa3c0c6e4076102a687be (patch) | |
| tree | 6a5ce9052d04000a063a540194ede78430a442e3 | |
| parent | 800fa0812a44efa8dee5915035cf62774b25d076 (diff) | |
[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>
| -rw-r--r-- | odoo/addons/test_new_api/tests/test_new_fields.py | 16 | ||||
| -rw-r--r-- | odoo/fields.py | 6 |
2 files changed, 21 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 b071e043414..c3a7005cab0 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -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. """ diff --git a/odoo/fields.py b/odoo/fields.py index 8b7cbfe71cc..6ad32a505b6 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -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) |
