Django:外键查询

时间:2015-10-13 10:21:33

标签: python django

我有models.py如下:

class Article(models.Model):
    board = models.ForeignKey(Board)
    subject = models.CharField("과목명", max_length=10)
    ...

class Question(models.Model):
    user = models.ForeignKey(User)
    article = models.ForeignKey(Article)
    professor = models.CharField("출제교수", max_length=4)
    ...

Views.py:

def get_q_table(request):
    context = {
        "q_list" : Question.objects.all().order_by("number")
    }
    return render(request, "q_table.html", context)

我想以某种方式将相关实例传递给q_table.html,但无法找到方法。我应该是下面的东西(当然,它不太正确。我一直在查找外键查询的文档,但我无法找到出路':

def get_q_table(request):
context = {
    "q_list" : Question.objects.all().order_by("number")
    "article" : Question.objects.all().related_board
}
return render(request, "q_table.html", context)

如果我找到出路,那么以下模板应该可以工作:

<div class="container">
    <div class="page-header"><h3>{{article.subject}} : {{article.year}}년 {{article.semester}}학기 {{article.quarter}}Q</h3></div>
    <div>
        <ul class="list-group" id="visitorbook">
        {% for question in questions %}
            <li class="list-group-item">{{question.number}} : {{ question.question }} - {{ question.answer }}
            </li>
        {% endfor %}
        </ul>
    </div>
</div>

4 个答案:

答案 0 :(得分:1)

在模板中,认为它应该是:

{% for question in q_list %}

答案 1 :(得分:1)

您已获取aricle对象;

articles = Article.objects.all()

{% for article in  articles %}
<div class="container">
     <div class="page-header"><h3>{{article.subject}} : {{article.year}}년 {{article.semester}}학기 {{article.quarter}}Q</h3></div>
     <div>
    <ul class="list-group" id="visitorbook">
     {% for question in article.question_set.all %}
        <li class="list-group-item">{{question.number}} : {{question.question }} - {{ question.answer }}
        </li>
      {% endfor %}
      </ul>
   </div>
</div>
{% endfor %}

如果您只想获取一篇文章,那么;

articles = Article.objects.get(id=id)`


<div class="container">
     <div class="page-header"><h3>{{article.subject}} : {{article.year}}년 {{article.semester}}학기 {{article.quarter}}Q</h3></div>
     <div>
    <ul class="list-group" id="visitorbook">
     {% for question in article.question_set.all %}
        <li class="list-group-item">{{question.number}} : {{question.question }} - {{ question.answer }}
        </li>
      {% endfor %}
      </ul>
   </div>
</div>

答案 2 :(得分:1)

我想明确选择你的文章,然后选择所有相关的问题,我想。

试试这个:

article = Article.objects.get(id)
questions = article.question_set.all()
context = {
    "article" : article
    "questions" : questions
}

使用相关对象的一般信息:https://docs.djangoproject.com/en/1.8/topics/db/queries/#related-objects

订购时:https://docs.djangoproject.com/en/1.8/ref/models/options/#ordering

答案 3 :(得分:1)

如果所有问题的文章相同,您可以尝试这样的事情:

{% with article=questions.0.article %}

<div class="container">
    <div class="page-header"><h3>{{article.subject}} : {{article.year}}년 {{article.semester}}학기 {{article.quarter}}Q</h3></div>
    <!-- other stuff -->
</div>

{% endwith %}

虽然您也应该以最适合您情况的方式处理问题列表为空的情况。另请注意,您不需要在上下文中添加“文章”。