[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
This commit is contained in:
@@ -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()}
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user