(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)
(我是python和django的新手,所以请耐心等一下。如果在其他地方已经找到并且找不到,我道歉
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.
假设我有一个Link模型,通过django-voting应用程序,用户可以对链接实例进行投票。如何根据他们的分数订购这些链接实例,例如。首先显示那些得分较高的人。
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).
我假设我可以使用django-voting的get_top管理器,但这只会给我最高评分链接实例,并且不会考虑我想添加的其他参数(例如,那些属于特定用户的链接)或分页或其他)。
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.
我的猜测是为我的Link模型编写一个自定义管理器,我可以根据每个项目的分数过滤一个查询集。如果我理解正确,将要求我遍历每个项目,检查其分数,然后将其放置一个列表(或字典),然后根据每个项目的分数对其进行排序。这不会返回查询集,而是返回每个项目的字典。
Am I missing something here?
我在这里错过了什么吗?
edit:
Here's a stripped-down version of the Link model:
这是Link模型的精简版:
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 a user votes I have this in my view:
当用户投票时,我在我看来有这个:
Vote.objects.record_vote(link, user, vote)
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.
其中link是Link实例,user是auth.User的实例,vote是1,0或-1。 ValidLinkManager只过滤掉is_deleted设置为True的链接。
2 个解决方案
#1
1
The get_top
method in VoteManager isn't that complicated. Look at its code (in managers.py:122). You can easily create a version of it that accepts a filter as another parameter and applies it to the "objects" queryset after it creates it, in line 158 - this way you can add other filters like the ones you're missing.
VoteManager中的get_top方法并不复杂。查看其代码(在managers.py:122中)。您可以轻松地创建一个版本,接受过滤器作为另一个参数,并在创建它之后将其应用于“对象”查询集,在第158行 - 这样您就可以添加其他过滤器,例如您缺少的过滤器。
Maybe you can also offer that as a patch back to the jonathan, and he'll put it in django-voting :)
也许你也可以把它作为补丁回到乔纳森,他会把它放在django投票:)
#2
0
I chose to use a generic relation in my model:
我选择在我的模型中使用泛型关系:
votes = generic.GenericRelation(Vote)
and then to aggregate it:
然后聚合它:
my_model.objects.annotate(num_votes=Count('votes'))
In this case I am just counting the number of votes received by the objects, but you can switch Count
with Avg
to get an average.
在这种情况下,我只计算对象收到的投票数,但您可以使用Avg切换Count以获得平均值。
Since this does not work with Django by default, I installed django-generic-aggregation
.
由于默认情况下这不适用于Django,我安装了django-generic-aggregation。
#1
1
The get_top
method in VoteManager isn't that complicated. Look at its code (in managers.py:122). You can easily create a version of it that accepts a filter as another parameter and applies it to the "objects" queryset after it creates it, in line 158 - this way you can add other filters like the ones you're missing.
VoteManager中的get_top方法并不复杂。查看其代码(在managers.py:122中)。您可以轻松地创建一个版本,接受过滤器作为另一个参数,并在创建它之后将其应用于“对象”查询集,在第158行 - 这样您就可以添加其他过滤器,例如您缺少的过滤器。
Maybe you can also offer that as a patch back to the jonathan, and he'll put it in django-voting :)
也许你也可以把它作为补丁回到乔纳森,他会把它放在django投票:)
#2
0
I chose to use a generic relation in my model:
我选择在我的模型中使用泛型关系:
votes = generic.GenericRelation(Vote)
and then to aggregate it:
然后聚合它:
my_model.objects.annotate(num_votes=Count('votes'))
In this case I am just counting the number of votes received by the objects, but you can switch Count
with Avg
to get an average.
在这种情况下,我只计算对象收到的投票数,但您可以使用Avg切换Count以获得平均值。
Since this does not work with Django by default, I installed django-generic-aggregation
.
由于默认情况下这不适用于Django,我安装了django-generic-aggregation。