【问题标题】:reddit style voting with django使用 django 进行 reddit 风格投票
【发布时间】:2011-02-26 08:49:58
【问题描述】:

我需要亲自将投票系统实现到模型中。

首先我得到了 Mike DeSimone 的大力帮助,但我需要扩展他的工作。

这是我当前的代码

查看

def show_game(request):
    game = Game.objects.get(pk=1)
    discussions = game.gamediscussion_set.filter(reply_to=None)
    d = {
        'game':game,
        'discussions':discussions
    }
    return render_to_response('show_game', d)

模板

<ul>
    {% for discussion in discussions %}
    {{ discussion.html }}
    {% endfor %}
</ul>

型号

class GameDiscussion(models.Model):
    game = models.ForeignKey(Game)
    message = models.TextField()
    reply_to = models.ForeignKey('self', related_name='replies', null=True, blank=True)
    created_on = models.DateTimeField(blank=True, auto_now_add=True)
    userUpVotes = models.ManyToManyField(User, blank=True, related_name='threadUpVotes')
    userDownVotes = models.ManyToManyField(User, blank=True, related_name='threadDownVotes')

    def html(self):
        DiscussionTemplate = loader.get_template("inclusions/discussionTemplate")
        return DiscussionTemplate.render(Context({
            'discussion': self,
            'replies': [reply.html() for reply in self.replies.all()]
    }))

讨论模板

<li>
    {{ discussion.message }}
    {% if replies %}
        <ul>
            {% for reply in replies %}
                {{ reply }}
            {% endfor %}
        </ul>
    {% endif %}
</li>

如您所见,我们在模型上有 2 个字段 userUpVotes 和 userDownVotes,它们将计算如何对讨论和回复进行排序。

我将如何实现这两个字段以根据投票对回复和讨论进行排序?

任何帮助都会很棒!

编辑

我在模型中添加了一个名为 vote_difference 的方法

    def vote_difference(self):
        return int(self.userUpVotes.count()) - int(self.userDownVotes.count())

我可以在我的模板中使用它来获得当前投票,但是我不能在我的 view.py 文件中使用它来按这个值排序,无论如何要在我的视图中包含这个值吗?

编辑(2)

我已经慢慢到达那里,我需要注释 2 个字段并对其进行计算,但是我似乎无法使用注释进行基本的数学计算。

有什么想法吗?

    discussions = game.gamediscussion_set.filter(reply_to=None).annotate( score= (Count('userUpVotes') - Count('userDownVotes')) ).order_by('-score')

【问题讨论】:

  • 那么你在这方面取得了多大的进步?

标签: django reddit vote


【解决方案1】:

您可能需要考虑通过添加vote_score 整数字段来稍微非规范化您的模型。

那么您所要做的就是覆盖save() 以使用您的vote_difference() 方法计算分数。

这使排序变得更加容易,并且可能会减少您正在进行的数据库调用次数。

【讨论】:

    【解决方案2】:

    reddit 算法是基于计算重力的公式。我从this website找到它

    Reddit 算法

    let t = (t1 – epoch)
    

    (其中 t1 是发布帖子的时间)

    let x be the number of up votes minus the number of down votes.
    

    那么,

    let y be:
    
    • 1 如果赞成票多于反对票,
    • -1 如果反对票多于赞成票,
    • 0 如果有相同的数字。

    现在让

    z = max({x,1})
    

    我们有

    ranking = C log10(z) + yt1
    
    Where C is a constant (C = 45000).
    

    【讨论】:

    • 嘿,感谢您的回复,肯定会在 reddits 的工作方式中添加一些现场内容。但是,您似乎错过了重点。我的模型有 2 个字段(userUpVotes 和 userDownVotes),我将计算这些值之间的差异以确定如何对我的讨论进行排序。 userUpVotes 可能有 8 个用户,而 userDownVotes 可能有 2 个。所以最后的投票是 userUpVotes - userDownVotes,在这种情况下可能是 +6。现在我需要帮助如何添加 userDownVotes 和 userUpVotes 字段,并将它们彼此分开并以此为基础进行排序。
    • 很抱歉没有将算法与 Django 相关联,我觉得这些信息会有所帮助,尽管我意识到它没有回答问题。
    【解决方案3】:

    我知道这不是您问题的直接答案。但是看看reddit's code 可能会很有帮助。当我不得不实现类似于 reddit 的半智能图像裁剪算法时,它帮助了我。

    【讨论】:

    • 缺点是它使用 pylons 框架而不是 django 框架:(
    【解决方案4】:

    我发布了一个名为 qhonuskan-votes 的投票应用程序,您可以从这里查看:https://github.com/miratcan/qhonuskan-votes

    【讨论】:

      猜你喜欢
      • 2012-01-24
      • 2023-03-15
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      相关资源
      最近更新 更多