[Answered ]-Django Filter Queryset by Highest Rating

2👍

I’m unsure whether you want highest average or highest sum so you may need to change the annotate

from django.db.models import Sum, Avg

Thing.objects.annotate(avg_rating=Avg('rating__score')).order_by('-avg_rating')
Thing.objects.annotate(sum_rating=Sum('rating__score')).order_by('-sum_rating')

see https://docs.djangoproject.com/en/dev/ref/models/querysets/#annotate

It looks like you are calling your VotingField rating which looks like its essentially a has many to the Vote model. So we want the Sum or Avg of each Thing‘s Vote collection’s score field. (I think its the score field we want to annotate https://github.com/Kronuz/django-ratings/blob/master/djangoratings/models.py#L15 but I don’t use this plugin so do a little research if this isn’t what you need)

Leave a comment