[Answered ]-Django Relation

1👍

Book and Author is ManyToMany relationship while book can have more then one authors and author can write more then one book. Therefore you need table AuthorBook in which you have records with Book and Author ids

1👍

You should have an intermediary model between author and book:

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    name = models.CharField(max_length=100)
    author = models.ManyToManyField(Person, through='AuthorBook')

class AuthorBook(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
👤lch

Leave a comment