如何在Django中列出从答案模型到问题模型的所有答案?多对一的关系?

2024-10-02 14:31:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我想请你帮忙

我有两个模型。问答。一个问题可以有很多答案。我的模型如下所示:

class Question(models.Model):
    question = models.CharField(max_length=300)
    answered = models.BooleanField(default=False)
    created = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    votesscore = models.IntegerField(default='0')
    amountofvotes = models.IntegerField(default='0')

    def __str__(self):
        return self.question


class Answer(models.Model):
    question_id = models.ForeignKey(Question, on_delete=models.CASCADE, blank=False, null=True)
    answer = models.TextField(max_length=1000)
    created = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    votesscore = models.IntegerField(default='0')
    amountofvotes = models.IntegerField(default='0')

    def __str__(self):
        return self.answer

以下是我的视图.py:

def home(request):
    allquestionswithanswers = Answer.objects.prefetch_related('question_id')
    allquestionswithoutanswers = Question.objects.filter(answered = False)
    return render(request, 'main/home.html', {'allquestionswithanswers': allquestionswithanswers, 'allquestionswithoutanswers': allquestionswithoutanswers})

这是my home.html:

<h1>Questions with answers:</h1>
<ul>
    {% for allquestionwithanswer in allquestionswithanswers %}
    <li>
        {{ allquestionwithanswer.question_id }} {{ allquestionwithanswer.user }}<br><br>
        <br><br>
        {{ allquestionwithanswer.answer }}
    </li>
    {% endfor %}

</ul>

<h1>Questions without answers:</h1>
<ul>
    {% for allquestionwithoutanswer in allquestionswithoutanswers %}
    <li>
        {{ allquestionwithoutanswer.question }} {{ allquestionwithoutanswer.user }}
        <br><br>
        {{ allquestionwithoutanswer.answer }}
    </li>
    {% endfor %}

</ul>

有些东西在工作,有些不工作,我不知道该怎么解决:(

我有“带答案的问题:”。它列出了我的问题和相应的答案,但如果问题有多个答案,它会多次打印问题,每次都打印下一个答案。我想这样看: -问题1 -答复1 -答复2 -问题1 -答复1

我知道我应该使用嵌套循环,但我尝试过它,但我无法让它工作

我的第二个问题是,在“没有答案的问题”中,我想列出布尔值“回答”的所有问题=False。但可以将其设置为False,但有一些答案,如果出现,我也想列出这些答案。但在这里,我对模型有问题,因为问题模型没有关于答案的信息。它只能以其他方式工作

我真的非常感谢任何帮助和指导

谢谢,干杯


Tags: 答案answerbrselffalsedefaultmodelsul
1条回答
网友
1楼 · 发布于 2024-10-02 14:31:04

要解决多次打印问题的问题,您必须查看循环实际执行的操作:

{% for allquestionwithanswer in allquestionswithanswers %}
    <li>
        {{ allquestionwithanswer.question_id }} {{ allquestionwithanswer.user }}
        <br><br><br><br>
        {{ allquestionwithanswer.answer }}
    </li>
{% endfor %}

在这里,您在allquestionswithanswers集合中循环-但是,这不是问题集合,而是答案集合!因此,您正在为集合中的每个答案执行循环,并在每次循环运行时打印出问题和答案(集合中的每个答案一个)

您需要从questions的集合中工作-您不正确地认为关系只以一种方式工作。您的问题知道与之相关的答案。事实上,有一个Django约定来检索特定问题的答案集。您将_set附加到模型的名称,在本例中它将是answer_set。对于您的模型,您可以使用question.answer_set.all的查询集来获取特定问题的所有答案

所以试试这个:

在你看来:

def home(request):
    allquestionswithanswers = Question.objects.filter(answered = True)
    allquestionswithoutanswers = Question.objects.filter(answered = False)

    return render(request, 'main/home.html', {'allquestionswithanswers': allquestionswithanswers, 'allquestionswithoutanswers': allquestionswithoutanswers})

在视图模板中:

<h1>Questions with answers:</h1>
<ul>
    {% for question in allquestionswithanswers %}
        <li>
            {{ question.id }} {{ question.user }}
            <br><br><br><br>
            {% for answer in question.answer_set.all %}
                {{ answer }}
            {% endfor %}
        </li>
    {% endfor %}
</ul>

<h1>Questions without answers:</h1>
<ul>
    {% for question in allquestionswithoutanswers %}
        <li>
            {{ question }} {{ question.user }}
            <br><br>
            {% for answer in question.answer_set.all %}
                {{ answer }}
            {% endfor %}
        </li>
    {% endfor %}
</ul>

让我知道这是怎么回事。另外,如果您还没有完成,请从头到尾完成Django tutorial。它指导您创建一个民意调查应用程序,该应用程序的模型结构与您的应用程序(问题/答案)几乎完全相同

相关问题 更多 >