Use empty value Function xxx2Many fields without initial value

Since d142b33fa3ab the on_change_with relies on _changed_value. But for
Function xxx2Many fields if their value is not in the initial value, we should
not retrieve it from the database because the result will be the same as the
one set by on_change_with.

Closes #12836
This commit is contained in:
Cédric Krier committed 2023-12-15 18:09:30 +01:00
1 parent 8197d55ed1
commit 0d437d341c
3 files changed
+24

No files matched your search

+2
View File
@@ -863,6 +863,8 @@ class ModelView(Model):
targets = value
if fname in init_values:
init_targets = init_values._get(fname)
elif isinstance(field, fields.Function):
init_targets = []
else:
init_targets = getattr(init_record, fname, [])
value = collections.defaultdict(list)
+6
View File
@@ -25,6 +25,12 @@ class ModelViewChangedValues(ModelView):
], "MultiSelection")
dictionary = fields.Dict(
'test.modelview.changed_values.dictionary', "Dictionary")
m2m_function = fields.Function(fields.Many2Many(
'test.modelview.changed_values.target', None, None, "Targets"),
'on_change_with_m2m_function')
def on_change_with_m2m_function(self, name=None):
return
class ModelViewChangedValuesDictSchema(DictSchemaMixin, ModelSQL):
+16
View File
@@ -109,6 +109,22 @@ class ModelView(unittest.TestCase):
},
})
@with_transaction()
def test_changed_values_2many_function(self):
"Test changed values for xxx2many function field"
pool = Pool()
Model = pool.get('test.modelview.changed_values')
with patch.object(
Model, 'on_change_with_m2m_function') as on_change_with:
on_change_with.return_value = [42]
record = Model()
record.m2m_function = record.on_change_with_m2m_function()
self.assertEqual(record._changed_values, {
'm2m_function': {'add': [(0, {'id': 42})]},
})
@with_transaction()
def test_changed_values_stored(self):
"Test stored changed values"