[IMP] website_forum: improve forum.post child_count performance.

PURPOSE

Several counter fields are performed inefficiently. They use compute methods
using length of 2many fields instead of making use of more efficient ORM
methods like read_group.

SPECIFICATIONS

No functional change, only technical performance improvements. Purpose is to
improve forum.post child_count performance.

LINKS

Task ID 2196182
PR #44662
This commit is contained in:
Victor Feyens
2020-04-21 12:28:52 +00:00
committed by Thibault Delavallée
parent 3f6625ef13
commit 9ff94fec74
+8 -7
View File
@@ -366,14 +366,15 @@ class Post(models.Model):
@api.depends('child_ids.create_uid', 'website_message_ids')
def _get_child_count(self):
def process(node):
total = len(node.website_message_ids) + len(node.child_ids)
for child in node.child_ids:
total += process(child)
return total
for post in self:
post.child_count = process(post)
children = self.search([('id', 'child_of', post.id)])
children_count = len(children)
message_count = self.env['mail.message'].search_count([
('model', '=', post._name),
('message_type', 'in', ['email', 'comment']),
('res_id', 'in', children.ids + [post.id]),
])
post.child_count = children_count + message_count
def _get_uid_has_answered(self):
for post in self: