diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 5083773bbea..93a99011520 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -239,7 +239,7 @@ class MailThread(models.AbstractModel): @api.multi def _compute_message_attachment_count(self): - read_group_var = self.env['ir.attachment'].read_group([('res_model', '=', self._name)], + read_group_var = self.env['ir.attachment'].read_group([('res_id', 'in', self.ids), ('res_model', '=', self._name)], fields=['res_id'], groupby=['res_id']) diff --git a/odoo/addons/base/models/ir_attachment.py b/odoo/addons/base/models/ir_attachment.py index 40acf9be995..535605d9fe7 100644 --- a/odoo/addons/base/models/ir_attachment.py +++ b/odoo/addons/base/models/ir_attachment.py @@ -381,6 +381,15 @@ class IrAttachment(models.Model): if not (self.env.user._is_admin() or self.env.user.has_group('base.group_user')): raise AccessError(_("Sorry, you are not allowed to access this document.")) + @api.model + def read_group(self, domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True): + """Override read_group to add res_field=False in domain if not present.""" + if not any(item[0] in ('id', 'res_field') for item in domain): + domain.insert(0, ('res_field', '=', False)) + return super(IrAttachment, self).read_group(domain, fields, groupby, + offset=offset, limit=limit, + orderby=orderby, lazy=lazy) + @api.model def _search(self, args, offset=0, limit=None, order=None, count=False, access_rights_uid=None): # add res_field=False in domain if not present; the arg[0] trick below