summaryrefslogtreecommitdiff
path: root/odoo
diff options
context:
space:
mode:
authorRaphael Collet <rco@odoo.com>2021-02-12 16:16:07 +0000
committerRaphael Collet <rco@odoo.com>2021-02-15 17:40:32 +0000
commit421631d7f222f1fcc3381c79320e109562533f22 (patch)
tree1e72ce90cb84a9402d2885ca7e8d9c13a91d34f6 /odoo
parent0820b9ca6b992675d62aaf21e3b14a6fe628652e (diff)
[FIX] core: flush domain before searching for records in many2many field
This guarantees the cache consistency of the many2many field values. X-original-commit: 74e188197e560897e13f7666d385a45ed7ca6f90
Diffstat (limited to 'odoo')
-rw-r--r--odoo/addons/test_new_api/tests/test_new_fields.py29
-rw-r--r--odoo/fields.py1
2 files changed, 28 insertions, 2 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 c8956245aa6..3efb67ce3e2 100644
--- a/odoo/addons/test_new_api/tests/test_new_fields.py
+++ b/odoo/addons/test_new_api/tests/test_new_fields.py
@@ -1767,8 +1767,8 @@ class TestFields(TransactionCaseWithUserDemo):
[('author_partner.name', '=', 'Marc Demo')])
self.assertEqual(messages, self.env.ref('test_new_api.message_0_1'))
- def test_60_x2many_domain(self):
- """ test the cache consistency of a x2many field with a domain """
+ def test_60_one2many_domain(self):
+ """ test the cache consistency of a one2many field with a domain """
discussion = self.env.ref('test_new_api.discussion_0')
message = discussion.messages[0]
self.assertNotIn(message, discussion.important_messages)
@@ -1782,6 +1782,31 @@ class TestFields(TransactionCaseWithUserDemo):
self.assertFalse(discussion.very_important_messages)
self.assertFalse(message.exists())
+ def test_60_many2many_domain(self):
+ """ test the cache consistency of a many2many field with a domain """
+ discussion = self.env.ref('test_new_api.discussion_0')
+ category = self.env['test_new_api.category'].create({'name': "Foo"})
+ discussion.categories = category
+ discussion.flush()
+ discussion.invalidate_cache()
+
+ # patch the many2many field to give it a domain (this simply avoids
+ # adding yet another test model)
+ field = discussion._fields['categories']
+ self.patch(field, 'domain', [('color', '!=', 42)])
+ self.registry.setup_models(self.cr)
+
+ # the category is in the many2many
+ self.assertIn(category, discussion.categories)
+
+ # modify the category; it should not longer be in the many2many
+ category.color = 42
+ self.assertNotIn(category, discussion.categories)
+
+ # modify again the category; it should be back in the many2many
+ category.color = 69
+ self.assertIn(category, discussion.categories)
+
def test_70_x2many_write(self):
discussion = self.env.ref('test_new_api.discussion_0')
# See YTI FIXME
diff --git a/odoo/fields.py b/odoo/fields.py
index 126108fc12b..fc159a3dc89 100644
--- a/odoo/fields.py
+++ b/odoo/fields.py
@@ -3507,6 +3507,7 @@ class Many2many(_RelationalMulti):
context.update(self.context)
comodel = records.env[self.comodel_name].with_context(**context)
domain = self.get_domain_list(records)
+ comodel._flush_search(domain)
wquery = comodel._where_calc(domain)
comodel._apply_ir_rules(wquery, 'read')
order_by = comodel._generate_order_by(None, wquery)