How to use python ORM of call-many relationship

  In models.py, I created two tables, they are the authors table and book table, and is many to many relationship between.

1  # Book 
2  class Book (models.Model):
 . 3      ID = models.AutoField (primary_key = True) # auto-increment primary key 
. 4      title = models.CharField (= 64 MAX_LENGTH, null = False, UNIQUE = True)   # Create a varchar (64) not only empty the title field 
. 5      Publisher = models.ForeignKey (to = ' Publisher ' , on_delete models.CASCADE =) # and the associated foreign key field Publisher 
. 6  
. 7  # oF - note book and oF should be many relationship 
. 8  class the Author (models.Model):
 . 9      ID = models.AutoField (primary_key = True)
 10     = models.CharField name (= 16 MAX_LENGTH, null = False, UNIQUE = True)
 . 11      # by the following statement, the Author table may be generated only, can automatically generate a third table 
12 is      Book = models.ManyToManyField (to = ' book ' ) # build relationships and many-author of the book

  After the implementation of the relevant code, build three tables:

  Wherein, app_author_book table shown below:

  Table author and book table by generating a third table, the respective contact id associated with a value.

  Now I would like a page to show all my Author:

1 # 展示作者
2 def author_list(request):
3 
4     author_obj = models.Author.objects.all()
5     return render(request,'author_list.html',{'author_list':author_obj})

  Then create a page author_list.html the value author_obj pass past:

 1 <table border="1">
 2     <thead>
 3     <tr>
 4         <th>编号</th>
 5         <th>ID</th>
 6         <th>作者姓名</th>
 7         <th>作品</th>
 8     </tr>
 9     </thead>
10     <tbody>
11     {% for author in author_list %}
12         <tr>
13         <td>{{ forloop.counter }}</td>
14         <td>{{ author.id }}</td>
15         <td>{{ author.name }}</td>
16         <td>
17             {% for book in author.book.all %}
18                 {% if forloop.last %}
19                     {{ book.title }}
20                  {% else %}
21                     {{ book.title }}|
22                 {% endif %}
23             {% endfor %}
24         </td>
25         </tr>
26     {% endfor %}
27     </tbody>
28 </table>

  The question is, because authors and books is many to many relationship, it may correspond to a multi-author book, a book may have multiple authors. I wanted to show his work at the back of each author, I joined a book in the Author field in the table models.py, I can call the data in the table Book by book field, such as 17 lines of code shown above, I wrote It is author.book.all, why write .all, rather than directly writing author.book it, precisely because an author may have multiple books, you can display the data of all the books by all, but if you write only author. book, the result is displayed as None. So remember, if it is to-many relationship, do not forget to add all remember oh.

  Simple results below:

 

Guess you like

Origin www.cnblogs.com/missdx/p/11200081.html