Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django通过向上/向下投票(如Hacker News或Reddit)对评论进行线程化处理_Django_Threaded Comments_Django Voting - Fatal编程技术网

Django通过向上/向下投票(如Hacker News或Reddit)对评论进行线程化处理

Django通过向上/向下投票(如Hacker News或Reddit)对评论进行线程化处理,django,threaded-comments,django-voting,Django,Threaded Comments,Django Voting,我是Django的新手。我正试图在Django中构建一个讨论应用程序,它与Reddit和Hacker News一样,是完全线程化的,并通过向上/向下投票对每条评论进行排序 我一直在使用django投票应用程序,如果可能的话,我想继续使用它 my models.py的简化版本为: Class Comment(models.Model): text = models.TextField() user = models.ForeignKey(User) parent = mod

我是Django的新手。我正试图在Django中构建一个讨论应用程序,它与Reddit和Hacker News一样,是完全线程化的,并通过向上/向下投票对每条评论进行排序

我一直在使用django投票应用程序,如果可能的话,我想继续使用它

my models.py的简化版本为:

Class Comment(models.Model):
    text = models.TextField()
    user = models.ForeignKey(User)
    parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
因为我使用的是django投票应用程序,所以我可以获得任何特定评论的“分数”(上升票减去下降票),如:

comment = Comment.objects.get(pk=1) # Grab the comment with primary key = 1
score = Vote.objects.get_score(comment)["score"]
我不明白的是如何: (a) 以可以转换为模板中的线程讨论的格式在视图中准备数据,以及 (b) 如何以按选票排序的方式进行排序

在这两个问题上的任何帮助都将不胜感激

我愿意使用另一种方法,比如mptt,来构建注释树——但我仍然不清楚如何在django mptt应用程序中按upvote排序

谢谢

编辑: 我已经想出了我自己的,非常黑客的解决方案如下。我不打算将这个问题标记为已回答,因为我不认为这是一种在生产中使用的解决方案(我希望自己不要在生产中使用)。但是,万一有人在寻找解决方案,我希望这会有所帮助:

在my views.py中,我创建了一个函数,在给定对象查询集的情况下,通过投票输出排序列表:

def list_sorted_by_score(query_set):
    sorted_score_list = []

    # First, I create a list of all the objects with their associated scores
    for object in query_set:
        # django-voting's get_score outputs a dictionary that looks like:
        # {"score": score, "num_votes": num_votes}
        score_dict = Vote.objects.get_score(object)
        sorted_score_list.append({"object": object, "score": score_dict["score"], "num_votes": score_dict["num_votes"]})

    # Then, I sort that list of dictionaries by the "score" 
    sorted_score_list.sort(key=lambda x:x["score"], reverse=True) # This sorts by score, highest to lowest (reverse=False is lowest to highest)
    return sorted_score_list
我使用该函数按分数对所有顶级评论进行排序。因此,我的上下文变量只包括按分数排序的注释,这些注释没有父项,即:

在my models.py中,我定义了一个方法,该方法通过给定注释的子项的得分对列表进行排序:

def score_list_children(self):
    children = self.children.all()
    sorted_score_list = []
    for object in children:
        score_dict = Vote.objects.get_score(object)
        sorted_score_list.append({"object": object, "score": score_dict["score"], "num_votes": score_dict["num_votes"]})
    sorted_score_list.sort(key=lambda x:x["score"], reverse=True)
    return sorted_score_list
最后,我为一条注释创建了一个模板“comment.html”。它的简化版本如下所示:

<div class="comment">
    {{ comment.object.text }} 
    {# note that I must use the comment**.object**, since 
    that's how I constructed my list_sorted_by_score list of dictionaries  #}
    {% for child in comment.object.score_list_children %}
         {% with filename="comment.html" %} {# hack to get around Django throwing a maximum recusion error #}
         {% include filename with comment=child %}
         {% endwith %}
    {% endfor %} 
</div>

{{comment.object.text}
{#注意,我必须使用注释**.object**,因为
这就是我如何构建我的列表(按分数排序的词典列表)
{comment.object.score_list_children%}
{%with filename=“comment.html”%}{#破解Django抛出的最大递归错误#}
{%include filename with comment=child%}
{%endwith%}
{%endfor%}

很明显,这是一种非常粗俗的做法。我仍然对听到人们在现实世界中尝试过的真实解决方案非常感兴趣。

对我来说,这似乎是一个合理的解决方案。你为什么认为这是一种黑客行为?
<div class="comment">
    {{ comment.object.text }} 
    {# note that I must use the comment**.object**, since 
    that's how I constructed my list_sorted_by_score list of dictionaries  #}
    {% for child in comment.object.score_list_children %}
         {% with filename="comment.html" %} {# hack to get around Django throwing a maximum recusion error #}
         {% include filename with comment=child %}
         {% endwith %}
    {% endfor %} 
</div>