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

Python 如何在Django中列出从答案模型到问题模型的所有答案?多对一的关系?,python,django,for-loop,django-models,Python,Django,For Loop,Django Models,我想请你帮忙 我有两个模型。问答。一个问题可以有很多答案。我的模型如下所示: 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(

我想请你帮忙

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

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>
带答案的问题:
    {所有问题中有答案的所有问题的百分比均高于答案%}
  • {{allquestionwithanswer.question_id}{{{allquestionwithanswer.user}}



    {{allquestionwithanswer.answer}
  • {%endfor%}
没有答案的问题:
    {在所有问题中没有答案的所有问题的百分比为%,而回答者为%}
  • {{allquestionwithoutanswer.question}}{{{allquestionwithoutanswer.user}

    {{allquestionwithoutanswer.answer}
  • {%endfor%}
有些东西起作用了,有些不起作用了,我不知道该如何修复它们:(

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

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

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

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


谢谢大家,干杯!

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

{% for allquestionwithanswer in allquestionswithanswers %}
    <li>
        {{ allquestionwithanswer.question_id }} {{ allquestionwithanswer.user }}
        <br><br><br><br>
        {{ allquestionwithanswer.answer }}
    </li>
{% endfor %}
在视图模板中:

<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>
带答案的问题:
    {所有问题中的问题百分比超过回答者%}
  • {{question.id}{{question.user}



    {问题中的答案为%answer\u set.all%} {{答案} {%endfor%}
  • {%endfor%}
没有答案的问题:
    {对于所有问题中的问题,答案为%}
  • {{question}}{{question.user}

    {问题中的答案为%answer\u set.all%} {{答案} {%endfor%}
  • {%endfor%}

让我知道它是如何工作的。此外,如果您还没有,请从头到尾完成。它将引导您创建一个民意调查应用程序,该应用程序的模型结构与您的应用程序(问题/答案)几乎完全相同.

非常感谢!它正在工作。我不知道为什么我会被困在这个答案和问题id上。从一开始就很愚蠢。我将阅读您链接的教程。我还将阅读有关“\u set.all”的内容。谢谢!:)很高兴我能帮上忙!
<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>