1. Django帮我们生成第三张表
class Author(models.Model):
name = models.CharField(max_length=32)
book = models.ManyToManyField('Book')
2. 自己创建第三张表
class AuthorBook(models, models):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
date = models.DateField()
3. 自建的表和Django自动生成的表联合使用
class Author(models.Model):
name = models.CharField(max_length=32)
book = models.ManyToManyField('Book', through='AuthorBook', through_fields=['author', 'book'])
class AuthorBook(models, models):
author = models.ForeignKey(Author, related_name='a', on_delete=models.CASCADE)
book = models.ForeignKey(Book, on_delete=models.CASCADE)
date = models.DateField()
reference = models.ForeignKey(Author, related_name='b', on_delete=models.CASCADE)
|