From be100b8b6cfdb79f9d77d001f9d747377593570c Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Fri, 12 Feb 2021 16:16:07 +0000 Subject: [PATCH] [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) --- .../test_new_api/tests/test_new_fields.py | 29 +++++++++++++++++-- odoo/fields.py | 1 + 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 3535a999608..c4c04982688 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -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 diff --git a/odoo/fields.py b/odoo/fields.py index 012a1a09bbf..442f23ef3f6 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -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)