使用 django 和 django-voting 应用程序,如何根据每个项目的投票来排序查询集?
(我是 python 和 django 的新手,所以请耐心等待。如果这个问题已在其他地方得到解答但找不到它,我深表歉意)
假设我有一个 Link 模型,并且通过 django 投票应用程序,用户可以投票在链接实例上。我如何根据它们的分数对这些链接实例进行排序,例如。首先显示分数较高的。
我假设我可以使用 django-voting 的 get_top 管理器,但这只会给我得分最高的链接实例,并且不会考虑我想添加的其他参数(例如,属于特定用户的那些链接)或寻呼或其他)。
我的猜测是为我的链接模型编写一个自定义管理器,在其中我可以根据每个项目的分数过滤查询集。如果我理解正确,这将需要我循环遍历每个项目,检查其分数,然后将其放置一个列表(或字典),然后根据每个项目的分数进行排序。这不会返回查询集,而是返回包含每个项目的字典。
我在这里错过了什么吗?
编辑:
这是 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()
当用户投票时,我的观点是:
Vote.objects.record_vote(link, user, vote)
其中 link 是 Link 实例,user 是 auth.User 的实例,而 vote 是 1、0 或 - 1. ValidLinkManager 只是过滤掉那些 is_deleted 设置为 True 的链接。
(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)
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.
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).
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.
Am I missing something here?
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
VoteManager 中的
get_top
方法并不复杂。看看它的代码(在managers.py:122中)。您可以轻松创建它的一个版本,该版本接受过滤器作为另一个参数,并在创建后将其应用于“对象”查询集(第 158 行) - 这样您就可以添加其他过滤器,例如您缺少的过滤器。也许你也可以将其作为补丁提供给乔纳森,他会将其放入 django-voting :)
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.Maybe you can also offer that as a patch back to the jonathan, and he'll put it in django-voting :)
我选择在模型中使用通用关系:
然后对其进行聚合:
在本例中,我只是计算对象收到的投票数,但您可以将
Count
与Avg 切换
获得平均值。因为 默认情况下这不适用于 Django< /a>,我安装了
django-generic-aggregation
。I chose to use a generic relation in my model:
and then to aggregate it:
In this case I am just counting the number of votes received by the objects, but you can switch
Count
withAvg
to get an average.Since this does not work with Django by default, I installed
django-generic-aggregation
.