From b3e8560c9c6469fc2ee5e908adea436c132b81da Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 7 Jul 2023 09:14:03 +0000 Subject: [PATCH] [FIX] tests: make first call to onchange2() return complete x2many value The use-case is a first call to onchange2() where: - a one2many field has a default value with a new line - some onchange method discards that line The diff should not return a "delete" command for the discarded line, since the client does not know about it. Instead, for that first call, it should behave like if the initial value of the field was empty. Part-of: odoo/odoo#127718 --- addons/web/models/models.py | 2 +- odoo/addons/test_testing_utilities/models.py | 13 ++++++++++--- .../test_testing_utilities/tests/test_form_impl.py | 6 ++++++ 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/addons/web/models/models.py b/addons/web/models/models.py index 6dbc29f6de1..b109bb69686 100644 --- a/addons/web/models/models.py +++ b/addons/web/models/models.py @@ -1196,7 +1196,7 @@ class RecordSnapshot(dict): commands = [] self_value = self[field_name] - other_value = other.get(field_name) or {} + other_value = {} if force else other.get(field_name) or {} if any(other_value): # other may be a snapshot for a real record, adapt its x2many ids other_value = {NewId(id_): snap for id_, snap in other_value.items()} diff --git a/odoo/addons/test_testing_utilities/models.py b/odoo/addons/test_testing_utilities/models.py index 2c6207d0335..b08f306fb20 100644 --- a/odoo/addons/test_testing_utilities/models.py +++ b/odoo/addons/test_testing_utilities/models.py @@ -175,13 +175,20 @@ class O2MDefault(models.Model): _name = 'test_testing_utilities.default' _description = 'Testing Utilities Default' + value = fields.Integer(default=1) + v = fields.Integer() + subs = fields.One2many('test_testing_utilities.sub3', 'parent_id', default=lambda self: self._default_subs()) + def _default_subs(self): return [ Command.create({'v': 5}) ] - value = fields.Integer(default=1) - v = fields.Integer() - subs = fields.One2many('test_testing_utilities.sub3', 'parent_id', default=_default_subs) + + @api.onchange('value') + def _onchange_value(self): + if self.value == 42: + self.subs = False + class O2MSub3(models.Model): _name = 'test_testing_utilities.sub3' diff --git a/odoo/addons/test_testing_utilities/tests/test_form_impl.py b/odoo/addons/test_testing_utilities/tests/test_form_impl.py index 277801b0384..237b2e34dce 100644 --- a/odoo/addons/test_testing_utilities/tests/test_form_impl.py +++ b/odoo/addons/test_testing_utilities/tests/test_form_impl.py @@ -411,6 +411,12 @@ class TestO2M(TransactionCase): self.assertEqual(s.value, 2) self.assertEqual(s.v, 2, "should have onchanged value to v") + def test_o2m_default_discarded(self): + """ Tests what happens when the default value is discarded. """ + model = self.env['test_testing_utilities.default'] + with Form(model.with_context(default_value=42)) as f: + self.assertFalse(len(f.subs)) + def test_o2m_onchange_parent(self): """ Tests that changing o2m content triggers onchange in the parent """