Create a classic Many-to-many relationship: A book can have multiple authors, and an author can have multiple books (as follows)
For data migrations, we then use the Python manage.py sqlmigrate app name to migrate the file name to view the SQL statements (as follows):
You can see from the diagram that three tables are generated, one is the book (books) table contains id,title two fields, one is author (the author table) contains id,name,email three fields, This is what we've just created two models in the models.py file, but one thing to be aware of is that there are no authors tables in the Book table, but a book_authors table with two more fields in the table Book_id,author_ ID, in fact, this third table is the middle table for storing the mapping between books and authors.
So how do we make inquiries about the data.
1. All authors of a book
b = Book.objects.get (id=1)
B.author.all ()
2. All books of an author:
A = Author.objects.get (id=1)
A.book_set.all ()
3. Add values to multiple fields (add Many-to-many Relationships):
To delete a value from a many-to-many field (delete a many-to-many relationship):
You can see that this blog_book_authors is a relational table that is automatically generated from a many-to-many relationship, but if we want to collect an additional field for the time when the author publishes a book, or if we are integrating with an existing system, the relational table already exists, and for this scenario, Django allows you to specify an intermediate model for managing Many-to-many relationships, and then add these additional fields to the intermediate model by specifying the through parameter in the Manytomany field to specify the intermediate model for the mediation, and modify the above models.py:
Check the database table structure at this point:
The custom name is used at the end of the creation of the Bookauthor class, which can be automatically generated by the system without specifying the table name
Look closely at the class of Bookauthor, the intermediate model we talked about, and we see that we created two foreign keys when we created the intermediate model, and the two foreign keys define how the two models relate to each other.
Therefore, when creating a many-to-many relationship model, we advocate using the through parameter to specify and create the intermediate model, which makes it more convenient for us to expand the field.
So how do we add and delete a many-to-many relationship at this point in time? Can you use the same method as you just did?
# Add author Ringo
Ringo = Author.objects.create (name= ' Ringo ', email= ' ringo@qq.com ')
# Add author Paul
Paul = Author.objects.create (name= ' Paul ', email= ' paul@qq.com ')
# Add Books Python Book1
Book1 = Book.objects.create (title= ' python book1 ')
# to add a value to multiple pairs is to add a many-to-many relationship
M1 = Bookauthor (Author=ringo,book=book)
# The second way of adding
M2 = bookauthor.objects,create (Author=paul,book=book1)
When we use the Many-to-many intermediate model, add (), remove (), create () are disabled, so the only way to create this type of relationship is by creating an instance of the intermediate model