|
|
""" Compute avg and count in one query, as thoses fields will be used together most of the time. """
domain = self._rating_domain() & Domain('rating', '>=', rating_data.RATING_LIMIT_MIN)
read_group_res = self.env['rating.rating']._read_group(domain, ['res_id'], aggregates=['__count', 'rating:avg'])
mapping = {res_id: {'rating_count': count, 'rating_avg': rating_avg} for res_id, count, rating_avg in read_group_res}
for record in self:
record.rating_count = mapping.get(record.id, {}).get('rating_count', 0)
record.rating_avg = mapping.get(record.id, {}).get('rating_avg', 0)
|
|