Your other chin
In: development
16 Jan 2013Sorting objects rated using django-secretballot is a little tricky. Simply using order_by causes duplicates. There’s even a warning about it in the Django manual for QuerySet.
I figured out how to rank the objects and avoid duplicates. It’s not that elegant, so if you have a better solution I’d love to hear it.
from django.db.models import Sum
photo_list = Photo.objects.annotate(total_votes=Sum('votes')).order_by('-total_votes')
I'm Jon Chin. I love technology, food, and learning. I served a mission in the Philippines and loved it. You probably can't type on my keyboard because I don't have qwerty installed--I use Colemak. I'm obsessed with learning about North Korea and abandoned anything.