Python 无法在我的模板中呈现ForeignKey字段

Python 无法在我的模板中呈现ForeignKey字段,python,django,django-models,django-templates,django-views,Python,Django,Django Models,Django Templates,Django Views,我正在为我的django应用程序制作一个评论系统,有人告诉我最好为评论投票制作一个单独的模型。我已经完成了,下面是我的模型。py: class Comment(models.Model): user = models.ForeignKey(User, default=1) destination = models.CharField(default='1', max_length=12, blank=True) author = model

我正在为我的django应用程序制作一个评论系统,有人告诉我最好为评论投票制作一个单独的模型。我已经完成了,下面是我的模型。py

    class Comment(models.Model):
        user = models.ForeignKey(User, default=1)
        destination = models.CharField(default='1', max_length=12, blank=True)
        author = models.CharField(max_length=120, blank=True)
        comment_id = models.IntegerField(default=1)
        parent_id = models.IntegerField(default=0)
        comment_text = models.TextField(max_length=350, blank=True)
        timestamp = models.DateTimeField(default=timezone.now, blank=True)

        def __str__(self):
            return self.comment_text


    class CommentScore(models.Model):
        user = models.ForeignKey(User, default=1)
        comment = models.ForeignKey(Comment, related_name='score')
        upvotes = models.IntegerField(default=0)
        downvotes = models.IntegerField(default=0)

        def __str__(self):
            return str(self.comment)
下面是我的视图.py,其中创建了注释:

def article(request, category, id):

    name = resolve(request.path).kwargs['category']
    for a, b in CATEGORY_CHOICES:
        if b == name:
            name = a
            instance = get_object_or_404(Post, id=id, category=name)

    allauth_login = LoginForm(request.POST or None)
    allauth_signup = SignupForm(request.POST or None)


    #comments
    comment = CommentForm(request.POST or None)
    ajax_comment = request.POST.get('text')
    comment_length = len(str(ajax_comment))

    comment_list = Comment.objects.filter(destination=id)
    score = CommentScore.objects.filter(comment=comment_list)

    if request.is_ajax():
        if comment.is_valid():
            comment = Comment.objects.create(comment_text=ajax_comment, author=str(request.user), destination=id)
            print(comment)
            comment.save()

            score = CommentScore.objects.create(comment=comment)
            score.save()
            username = str(request.user)
            return JsonResponse({'text': ajax_comment, 'text_length': comment_length, 'username': username})
        else:
            print(comment.errors)

    context = {
        'score': score,
        'comment_list': comment_list,
        'comment': comment,
        'instance': instance,
        'allauth_login': allauth_login,
        'allauth_signup': allauth_signup
    }

    return render(request, 'article.html', context)
因此,
comment
可以正常工作,但正如您在后面几行看到的那样,我正在尝试创建一个
CommentScore
实例以与
comment
匹配。在我的模板中,我已经呈现了每个注释及其字段(
comment\u text
author
等),但我想呈现与该
注释相关联的
upvows
字段。我该怎么做

模板

{% for i in comment_list %}
    <div class='comment_div'>
        <h3>{{ i.author }}</h3>
        <p>{{ i.comment_text }}</p><br>
    </div>
{% endfor %}
我已经尝试过以下方法,但都没有效果

{% for i in comment_list %}
    <div class='comment_div'>
        <h3>{{ i.author }}</h3>
        <p>{{ i.comment_text }}</p><br>
            {% for i in comment_list.score_set.all %}
                {{ i.upvotes }} #renders nothing
            {% endfor %}
    </div>
{% endfor %}

{% for i in comment_list %}
    <div class='comment_div'>
        <h3>{{ i.author }}</h3>
        <p>{{ i.comment_text }}</p><br>
            {% for j in i.score %}
                {{ j.upvotes }} #Error: 'RelatedManager' object is not iterable
            {% endfor %}
    </div>
{% endfor %}
{comment_list%中的i的%
{{i.作者}
{{i.comment_text}


{comment_list.score_set.all%} {{i.upvoces}}}什么也不做 {%endfor%} {%endfor%} {注释列表%中的i的% {{i.作者} {{i.comment_text}


{i.score%中j的百分比} {{j.upvoals}}#错误:“RelatedManager”对象不可编辑 {%endfor%} {%endfor%}
遇到很多麻烦,因此非常感谢您的帮助。

将“i.score”更改为“i.score.all”可以解决问题,因为当您尝试迭代管理器而不是该管理器选择的对象时,通常会发生相关管理器错误。-由@joe-j解决

现在可以了,但是如果有人能解释一下这个语法的第二行,那就太好了:

comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.filter(comment=comment_list)
当我在这里指定
comment=comment\u list
时,到底发生了什么?我从其他人那里复制了这段代码,但我仍然有点不确定它是如何工作的

将“i.score”更改为“i.score.all”可以解决此问题,因为当您尝试迭代管理器而不是该管理器选择的对象时,通常会发生RelatedManager错误。-由@joe-j解决

现在可以了,但是如果有人能解释一下这个语法的第二行,那就太好了:

comment_list = Comment.objects.filter(destination=id)
score = CommentScore.objects.filter(comment=comment_list)

当我在这里指定
comment=comment\u list
时,到底发生了什么?我从其他人那里复制了这段代码,但我仍然有点不确定它是如何工作的

尝试将“j.upvowers”更改为“j.upvowers.all”,我会得到相同的错误。也许可以尝试将“I.score”更改为“I.score.all”。relatedmanager错误通常发生在您尝试迭代管理器而不是该管理器选择的对象时。就是这样,它现在可以工作了。非常感谢。很高兴它有帮助!尝试将“j.upvowers”更改为“j.upvowers.all”,我会得到相同的错误。也许可以尝试将“I.score”更改为“I.score.all”。relatedmanager错误通常发生在您尝试迭代管理器而不是该管理器选择的对象时。就是这样,它现在可以工作了。非常感谢。很高兴它有帮助!