Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing django and django-voting app, how can I order a queryset according to the votes of each item?
    primarykey
    data
    text
    <p>(I'm new to python and django so please bear with me for a second. I apologise if this has been answered elsewhere and couldn't find it)</p> <p>Let's say I have a Link model and through the django-voting application users can vote on link instances. How can I order those link instances according to their score, eg. display those with the higher score first.</p> <p>I assume I could use the get_top manager of django-voting, but that would only give me the top scoring link instances and wouldn't take into consideration other parameters I would like to add (for example, those links that belong to a specific user or paging or whatever).</p> <p>My guess would be to write a custom manager for my Link model where by I can filter a queryset according to each item's score. If I understand correctly that will require me to loop through each item, check its score, and then place it a list (or dictionary) which will then be sorted according to the score of each item. That wouldn't return a queryset but a dictionary with each item.</p> <p>Am I missing something here?</p> <p>edit:</p> <p>Here's a stripped-down version of the Link model:</p> <pre><code>class Link(models.Model): user = models.ForeignKey('auth.User') category = models.ForeignKey(Category) date = models.DateTimeField( auto_now_add=True, null=True, blank=True ) is_deleted = models.BooleanField(default=False, blank=True) links = ValidLinkManager() objects = models.Manager() </code></pre> <p>and when a user votes I have this in my view:</p> <pre><code>Vote.objects.record_vote(link, user, vote) </code></pre> <p>where link is the Link instance, user is an instance of auth.User and vote is either 1, 0, or -1. The ValidLinkManager just filters out those links that have is_deleted set to True.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload