[FIX] core: flush domain before searching for records in many2many field

This guarantees the cache consistency of the many2many field values.

closes odoo/odoo#66299

X-original-commit: 421631d7f222f1fcc3381c79320e109562533f22
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
This commit is contained in:
Raphael Collet
2021-02-16 17:38:11 +00:00
parent 2a679ac0a8
commit be100b8b6c
2 changed files with 28 additions and 2 deletions
@@ -1770,8 +1770,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)
@@ -1785,6 +1785,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
+1
View File
@@ -3632,6 +3632,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)