{"id":244,"date":"2013-01-16T09:38:08","date_gmt":"2013-01-16T17:38:08","guid":{"rendered":"http:\/\/otherchin.com\/blog\/?p=244"},"modified":"2013-01-16T09:38:08","modified_gmt":"2013-01-16T17:38:08","slug":"sorting-objects-by-django-secretballot-votes","status":"publish","type":"post","link":"http:\/\/otherchin.com\/blog\/2013\/01\/sorting-objects-by-django-secretballot-votes\/","title":{"rendered":"Sorting objects by django-secretballot votes"},"content":{"rendered":"<p>Sorting objects rated using django-secretballot is a little tricky. Simply using order_by causes duplicates. There&#8217;s even a warning about it in the <a title=\"distinct()\" href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/models\/querysets\/#distinct\" target=\"_blank\">Django manual for QuerySet<\/a>.<\/p>\n<p>I figured out how to rank the objects and avoid duplicates.  It&#8217;s not that elegant, so if you have a better solution I&#8217;d love to hear it.<br \/>\n<code><br \/>\nfrom django.db.models import Sum<\/p>\n<p>photo_list = Photo.objects.annotate(total_votes=Sum('votes')).order_by('-total_votes')<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorting objects rated using django-secretballot is a little tricky. Simply using order_by causes duplicates. There&#8217;s even a warning about it in the Django manual for QuerySet. I figured out how to rank the objects and avoid duplicates. It&#8217;s not that elegant, so if you have a better solution I&#8217;d love to hear it. from django.db.models [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[171,169,170],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-development","tag-django","tag-django-secretballot","tag-python"],"_links":{"self":[{"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":2,"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":246,"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/posts\/244\/revisions\/246"}],"wp:attachment":[{"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/otherchin.com\/blog\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}