评论已正确分配给答案对象,但显示在一个答案对象中

问题描述 投票:0回答:1

我正在建立一个问答平台来帮助公司了解客户的需求。正如您在

view_question
视图中看到的,我正在显示基于问题的答案,并且它运行良好。但是,当使用此方法显示对答案的评论时:

answers = Answer.objects.filter(post=question)
answers_comment = CommentA.objects.filter(post=answers)

此方法在模板中返回错误:

The QuerySet value for an exact lookup must be limited to one result using slicing.
然后我尝试使用此方法显示它:

answers = Answer.objects.filter(post=question)
answers_comment = CommentA.objects.filter(post__id__in=answers)

看似问题解决了,其实不然。

answers_comment
将所有评论对象显示到单个答案对象,即使它们未分配给该答案。

{% for answer in answers %}
        <div class="row border-bottom">
            <div class="col col-answer">
                <div class="answer-icons">
                    <a style="text-decoration: none;" href="">
                        <span><i class="fas fa-arrow-up"></i></span>
                        <span>{{ answer.up_vote }}</span>
                    </a>
    
                    <a style="text-decoration: none;" href="">
                        <span><i class="fas fa-arrow-down"></i></span>
                        <span>{{ answer.down_vote }}</span>
                    </a>
                </div>
                <div class="answer-text">
                    <p>{{answer.your_answer|convert_markdown|safe}}</p>
                </div>
            </div>
        </div>
        {% endfor %}



        <div class="comment-container m-2">
            {% for answer_comment in answers_comment %}
            <div class="row">
                <small>{{ answer_comment.comment }}</small>
                {% with answer_comment.user.account as account %}
                <a style="text-decoration: none;" class="user-avatar" href="{% url 'Public-Profile' slug=account.slug %}">
                    <span>{{ answer_comment.user.username }}
                    </span>
                </a>
                <small>{{ answer_comment.created_at|naturaltime }}</small>
                <hr>
                {% endwith %}
            </div>
            {% endfor %}
def view_question(request, slug):
    question = get_object_or_404(Question, slug=slug)
    answers = Answer.objects.filter(post=question)
    answers_comment = CommentA.objects.filter(post__id__in=answers)
    context = {"question":question, "answers":answers, "answers_comment":answers_comment}
    return render(request, 'view_question.html', context)

型号:

class Question(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    title = models.CharField(max_length=225)
    body = models.TextField()

class Answer(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    your_answer = models.TextField()
    post = models.ForeignKey(Question, on_delete=models.CASCADE)

class CommentA(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    comment = models.TextField()
    post = models.ForeignKey(Answer, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
python django django-templates
1个回答
0
投票

在您看来尝试以下操作

 answers_id = [a.id for a in answers]
 answers_comment = CommentA.objects.filter(post_id__in=answers)

这不会进行连接,因为

post_id
已在
CommentA
模型中。

注意:这里的问题是您在不知道它们属于哪个答案的情况下发送所有评论,这将使模板代码变得更加困难。

解决方案是使用django提供的反向关系查找,如下

for answer in answers:
    for comment in answer.commentA_set.all():
         .....
© www.soinside.com 2019 - 2024. All rights reserved.