miratcan / qhonuskan-votes

Simple reddit like voting system without usage off GenericForeignKeys. Built for linkfloyd project.
Other
30 stars 11 forks source link

Sort order #11

Closed magnetic-pi closed 11 years ago

magnetic-pi commented 11 years ago

Is there a way to manage the sort order by vote count? I was looking at the models.py and appended .order_by("-vote_score") to the end of the get_query_set line 24 below:

 14 class ObjectsWithScoresManager(models.Manager):                                                                                             
 15     """                                                                                                                                     
 16     Returns objects with their scores                                                                                                       
 17     """                                                                                                                                     
 18     def get_query_set(self):                                                                                                                
 19         from qhonuskan_votes.utils import SumWithDefault                                                                                    
 20         return super(ObjectsWithScoresManager, self).get_query_set().annotate(                                                              
 21             vote_score=SumWithDefault(                                                                                                      
 22                 '%svote__value' % self.model._meta.module_name, default=0                                                                   
 23             )                                                                                                                               
 24         ).order_by("-vote_score")

The above returns the desired results. Is there another way to sort by vote that I may be missing? Could there be a way to make this field sortable in a view. Example:

from qhonuskan_votes.models import *

something = aModel.objects_with_scores.all().order_by("-vote_score")

Then in the context all the user has to do is set:

'something': something

In your demo, the results were returned in the order of 1, 0, -1 respectively.

Selection_005

Could this be added by default and pushed to pip as version 1.8? I would really like to keep the plugin as vanilla as possible to make upgrading easier, so if you have another way of sorting by vote value, that would be awesome!

Thanks, Jon

miratcan commented 11 years ago

merged with your repo