class Book(models.Model): # 默認會建立id name = models.CharField(max_length=32) # 中介模型,手動指定第三張中間表是Book2Author authors=models.ManyToManyField(to='Author',through='Book2Author',through_fields= ('book','author')) class Author(models.Model): name = models.CharField(max_length=32) def __str__(self): return self.name class Book2Author(models.Model): id = models.AutoField(primary_key=True) book=models.ForeignKey(to='Book',to_field='id') author=models.ForeignKey(to='Author',to_field='id')
-終極總結:防止混了:關聯字段就是表名小寫,第一個值:就是當前表的表名小寫
-查詢,新增,刪除,都很方便
-第三張表,能夠添加別的字段code