【问题标题】:django-secretballot : How to get a QuerySet order_by votes score?django-secretballot:如何获得 QuerySet order_by 投票分数?
【发布时间】:2014-05-02 08:42:57
【问题描述】:

我有一个模型:

class Question(models.Model):
    title   = models.CharField(max_length=100)
    body = models.CharField(max_length=200)

secretballot.enable_voting_on(Question)

一切正常。但如果我希望这些问题按他们的投票分数排序,

就像 reddit 风格一样。运行:

Question.objects.filter(Terms_id=1).order_by('total_name')

得到这个错误:

FieldError:无法将关键字“total_name”解析为字段。选项有:title、body、id、votes

问题出在哪里?

【问题讨论】:

  • 试试.order_by('-votes')
  • @Rohan:.order_by('-votes') 不起作用。它返回一个包含所有投票的查询集:[<Question: dadfaet>, <Question: werwr>, <Question: zzxsddds>, <Question: some girl>, <Question: zzz,ggghhh>, <Question: some girl>, <Question: zzz,ggg hhh>, <Question: some girl>, <Question: zzz,ggghhh>, <Question: zzz,ggghhh>]
  • 但这不是按票数排序的吗?
  • 我希望它首先按票数排序,投票得分最高的问题对象,然后是第二个 .... 像 reddit,但 order_by('votes') 返回一个带有重复对象的 QuerySet

标签: django


【解决方案1】:

这对我有用:

Question.objects.order_by('-total_upvotes').distinct()

Distinct 将删除重复项。

【讨论】:

    猜你喜欢
    • 2022-08-10
    • 2022-09-28
    • 2011-03-04
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 2012-10-29
    • 2012-07-25
    • 2013-12-05
    相关资源
    最近更新 更多