简体   繁体   English

查询django中列表视图的外键表

[英]query foreign key table for list view in django

I am very new to django. 我是django的新手。 I have a django installation and am trying to create a UI in django for a few of the database tables. 我有一个django安装,我正在尝试在django中为一些数据库表创建一个UI。

In my database there are two tables - Articles and Author. 在我的数据库中有两个表 - 文章和作者。 The Articles table has a foreign key to Author table with field name as author_id. Articles表具有Author表的外键,字段名称为author_id。

I have managed to create a ListView class that lists articles. 我设法创建一个列出文章的ListView类。 The code is like this: 代码是这样的:

from .models import Article

class ArticleListView(ListView):
    template_name = "article.html"
    context_object_name = 'articles'
    paginate_by = 25

    def get_queryset(self):
        queryset = Article.objects.all().order_by('-created_at')
        return queryset 

Then in the view I loop the Article queryset and prints its fields, and this works fine. 然后在视图中我循环文章查询集并打印其字段,这很好。 However I am not sure how to query the corresponding Author table in order to get the author name and details. 但是我不知道如何查询相应的Author表以获取作者姓名和详细信息。 Can anyone please help me understand how this can be done? 谁能帮助我了解如何做到这一点? I read a lot of documentation/tutorials about this but I am unable to wrap my head around how to do this. 我阅读了很多关于此的文档/教程,但我无法解决如何做到这一点。 Any help is very much appreciated. 很感谢任何形式的帮助。

Please note: The Article model was written by earlier django programmer. 请注意:文章模型是由早期的Django程序员编写的。

If you define a ForeignKey with name column to another model, then Django will construct a database column with the name column_id to obtain the primary key of the related object. 如果将带有name columnForeignKey定义到另一个模型,则Django将构造一个名为column_id的数据库列,以获取相关对象的主键。 But you can use .column to obtain the related object (so not the id , but the corresponding object of that id ). 但是你可以用.column获取相关对象(所以不是id ,但相应的对象id )。

You can thus change the template, for example to: 您可以这样更改模板,例如:

<h1>Articles</h1>
<ul>
{% for article in object_list %}
    <li>{{ article.pub_date|date }} - {{ article.headline }}
         - {{article.author.name}}</li>
{% empty %}
    <li>No articles yet.</li>
{% endfor %}
</ul>

(given of course an author has a name field). (当然, author有一个name字段)。

Since you here will fetch all the author objects, it is usually more efficient to do a prefetch_related(..) on the queryset in that case: 由于您在此处将获取所有作者对象,因此在该情况下对查询集执行prefetch_related(..)通常更有效:

class ArticleListView(ListView):
    template_name = 'article.html'
    paginate_by = 25

    def get_queryset(self):
        return Article.objects..prefetch_related( 'author' ).order_by('-created_at')

You can thus call .author on any Article instance to obtain the Author object that relates to it (and for instance fetch properties of that author, modify the author, etc.). 因此,您可以在任何Article 实例上调用.author以获取.authorAuthor对象( 例如 ,获取该Author属性,修改作者等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM