Complete the code to define a many-to-many relationship between Author and Book models.
class Book(models.Model): title = models.CharField(max_length=100) class Author(models.Model): name = models.CharField(max_length=100) books = models.[1](Book)
The ManyToManyField is used to create a many-to-many relationship between models in Django.
Complete the code to add a related name to the ManyToManyField for reverse lookup.
class Author(models.Model): name = models.CharField(max_length=100) books = models.ManyToManyField(Book, [1]='authors')
The related_name attribute sets the name for the reverse relation from Book to Author.
Fix the error in the code to correctly define a many-to-many field with a through model.
class Membership(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) book = models.ForeignKey(Book, on_delete=models.CASCADE) date_joined = models.DateField() class Author(models.Model): name = models.CharField(max_length=100) books = models.ManyToManyField(Book, [1]='Membership')
The through attribute specifies the intermediate model for a many-to-many relationship.
Fill both blanks to create a many-to-many field with a custom through model and a related name.
class Author(models.Model): name = models.CharField(max_length=100) books = models.ManyToManyField(Book, [1]='Membership', [2]='authors')
Use through to specify the intermediate model and related_name for reverse lookup.
Fill all three blanks to define a many-to-many field with a through model, related name, and symmetrical set to False.
class Author(models.Model): name = models.CharField(max_length=100) books = models.ManyToManyField(Book, [1]='Membership', [2]='authors', [3]=False)
through sets the intermediate model, related_name sets reverse lookup, and symmetrical=False is used for many-to-many relations where the relation is not symmetrical (like followers).