[FIX] rating: count and average
In an app using rating (e.g. eLearning), get 3 ratings: - A 5-star review - A 3-star review - A 0-star review The average is 2.5 stars, while it should be 4 stars. This happens because the 0-star review is taken into account in the average computation, while it shouldn't. Indeed, zero star means no review. We apply the same login than: https://github.com/odoo/odoo/blob/0028a602bea6a48aaa2747127ec075394732b324/addons/rating/models/rating_mixin.py#L205 opw-2290617 closes odoo/odoo#54279 X-original-commit: 4f7981f80fb55ab4bf8b30d137e45b4fda4013cd Signed-off-by: Nicolas Martinelli (nim) <nim@odoo.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from odoo import api, fields, models, tools
|
||||
from odoo.addons.rating.models.rating import RATING_LIMIT_SATISFIED, RATING_LIMIT_OK
|
||||
from odoo.addons.rating.models.rating import RATING_LIMIT_SATISFIED, RATING_LIMIT_OK, RATING_LIMIT_MIN
|
||||
from odoo.osv import expression
|
||||
|
||||
|
||||
@@ -68,7 +68,7 @@ class RatingMixin(models.AbstractModel):
|
||||
@api.depends('rating_ids.res_id', 'rating_ids.rating')
|
||||
def _compute_rating_stats(self):
|
||||
""" Compute avg and count in one query, as thoses fields will be used together most of the time. """
|
||||
domain = self._rating_domain()
|
||||
domain = expression.AND([self._rating_domain(), [('rating', '>=', RATING_LIMIT_MIN)]])
|
||||
read_group_res = self.env['rating.rating'].read_group(domain, ['rating:avg'], groupby=['res_id'], lazy=False) # force average on rating column
|
||||
mapping = {item['res_id']: {'rating_count': item['__count'], 'rating_avg': item['rating']} for item in read_group_res}
|
||||
for record in self:
|
||||
|
||||
Reference in New Issue
Block a user