Three ways to create many-ways
1. Automatic (recommended *)
Advantage: You do not need to manually create the third table
Inadequate: As the third table than you created manually, which means the third table is fixed and can not do field expansion
class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8,decimal_places=2) authors = models.ManyToManyField(to='Author') class Author(models.Model): name = models.CharField(max_length=32)
2. Pure manual (to understand)
Create their own third table
Advantages: can any third extension field
Inadequate: orm query is not convenient
class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalField(max_digits=8,decimal_places=2) class Author(models.Model): name = models.CharField(max_length=32) class Book2Author(models.Model): book = models.ForeignKey(to='Book') author = models.ForeignKey(to='Author') create_time = models.DateField(auto_now_add=True)
3. Semi-Automatic (recommended *******)
Advantages: automatic and combines the advantages of two pure manual
class Book (models.Model): title = models.CharField (MAX_LENGTH = 32 ) . price = models.DecimalField (= max_digits. 8, decimal_places = 2 ) the authors = models.ManyToManyField (to = ' the Author ' , through = ' Book2Author ' , = through_fields ( ' book ' , ' author ' )) # through books tell django orm and author tables of many relationship is recorded by Book2Author to # through_fields tell django used book field Book2Author table when orm record and relationship author field to record "" " many to many fields in the Add the SET the Remove the Clear does not support """ class Author(models.Model): name = models.CharField(max_length=32) # books = models.ManyToManyField(to='Book', through='Book2Author', through_fields=('author', 'book')) class Book2Author(models.Model): book = models.ForeignKey(to='Book') author = models.ForeignKey(to='Author') create_time = models.DateField(auto_now_add=True)