diff --git a/base_comment_template/models/base_comment_template.py b/base_comment_template/models/base_comment_template.py index 37c0a5f54a..20ca6881f0 100644 --- a/base_comment_template/models/base_comment_template.py +++ b/base_comment_template/models/base_comment_template.py @@ -5,6 +5,7 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from odoo import _, api, fields, models from odoo.exceptions import ValidationError +from odoo.tools import Query class BaseCommentTemplate(models.Model): @@ -128,9 +129,15 @@ def name_get(self): def _search_model_ids(self, operator, value): # We cannot use model_ids.model in search() method to avoid access errors - allowed_items = ( - self.sudo() - .search([]) - .filtered(lambda x: value in x.model_ids.mapped("model")) - ) + all_items = self.sudo().search([]) + if isinstance(value, Query): + found_models = self.env["ir.model"].browse(value) + allowed_items = self.browse() + for found_model in found_models: + model_name = found_model.model + allowed_items |= all_items.filtered( + lambda x: model_name in x.models.split(",") + ) + else: + allowed_items = all_items.filtered(lambda x: value in x.models.split(",")) return [("id", "in", allowed_items.ids)] diff --git a/base_comment_template/tests/test_base_comment_template.py b/base_comment_template/tests/test_base_comment_template.py index 7ad31cdc7b..47c448c359 100644 --- a/base_comment_template/tests/test_base_comment_template.py +++ b/base_comment_template/tests/test_base_comment_template.py @@ -184,3 +184,27 @@ def test_partner_commercial_fields(self): self.assertTrue( "base_comment_template_ids" in self.env["res.partner"]._commercial_fields() ) + + def test_search_model_ids_model(self): + """Searching by "model_ids.model" + finds the comments matching the search criteria.""" + # Arrange + user_ir_model = self.user_obj + user_model_name = user_ir_model.model + user_comments_by_model_ids = self.env["base.comment.template"].search( + [ + ("model_ids", "=", user_model_name), + ] + ) + # pre-condition + self.assertTrue(user_comments_by_model_ids) + + # Act + user_comments_by_models_path = self.env["base.comment.template"].search( + [ + ("model_ids.model", "=", user_model_name), + ] + ) + + # Assert + self.assertEqual(user_comments_by_model_ids, user_comments_by_models_path)