Using the django and django-vote app, how can I order a set of requests according to the voices of each item?

(I am new to python and django, so please bear with me for a second. I apologize if it was answered elsewhere and could not find it)

Say I have a Link model, and through the django voting app, you can vote for link instances. How can I order these link instances according to their invoice, for example. first display those with the highest score.

I suppose I could use the get_top manager for django-voting, but that would give me only the best link instances and not take into account other parameters that I would like to add (for example, those links that belong to a specific user or paging or whatever something else).

My guess would be to write a custom manager for my Link model, where I can filter out a set of queries according to each number of points. If I understand correctly what will require me to scroll through each element, check its rating, and then put a list (or dictionary) in it, which will then be sorted according to the count of each element. This does not return a query, but a dictionary with each element.

Did I miss something?

edit:

Here's a stripped down version of the Link model:

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() 

and when the user votes, I have this in my opinion:

 Vote.objects.record_vote(link, user, vote) 

where the link is an instance of the link, the user is an instance of auth.User, and the vote is 1, 0, or -1. ValidLinkManager simply filters out links for which is_deleted is set to True.

+6
django sql-order-by
source share
2 answers

The get_top method in VoteManager is not that complicated. Look at its code (in managers .py: 122). You can easily create a version that takes the filter as another parameter and applies it to the query for “objects” after creating it on line 158 — so you can add other filters, such as the ones you are missing.

Maybe you can also suggest this as a patch for jonathan, and he will put it in a vote on django :)

+1
source share

I decided to use the general ratio in my model:

 votes = generic.GenericRelation(Vote) 

and then for its aggregation:

 my_model.objects.annotate(num_votes=Count('votes')) 

In this case, I just count the number of votes received by the objects, but you can switch Count to Avg to get the average value.

Since this does not work with Django by default , I installed django-generic-aggregation .

0
source share

All Articles