0
0
Djangoframework~10 mins

ManyToManyField for many-to-many in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a many-to-many relationship between Author and Book models.

Django
class Book(models.Model):
    title = models.CharField(max_length=100)

class Author(models.Model):
    name = models.CharField(max_length=100)
    books = models.[1](Book)
Drag options to blanks, or click blank then click option'
AOneToOneField
BForeignKey
CManyToManyField
DCharField
Attempts:
3 left
💡 Hint
Common Mistakes
Using ForeignKey instead of ManyToManyField
Using OneToOneField which is for one-to-one relations
2fill in blank
medium

Complete the code to add a related name to the ManyToManyField for reverse lookup.

Django
class Author(models.Model):
    name = models.CharField(max_length=100)
    books = models.ManyToManyField(Book, [1]='authors')
Drag options to blanks, or click blank then click option'
Arelated_name
Breverse_name
Cbackref
Drelated_query_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'backref' which is not a Django field option
Using 'reverse_name' which does not exist
3fill in blank
hard

Fix the error in the code to correctly define a many-to-many field with a through model.

Django
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')
Drag options to blanks, or click blank then click option'
Athrough
Brelated_name
Cthrough_model
Dintermediate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'through_model' which is not a valid attribute
Using 'related_name' instead of 'through'
4fill in blank
hard

Fill both blanks to create a many-to-many field with a custom through model and a related name.

Django
class Author(models.Model):
    name = models.CharField(max_length=100)
    books = models.ManyToManyField(Book, [1]='Membership', [2]='authors')
Drag options to blanks, or click blank then click option'
Athrough
Brelated_name
Crelated_query_name
Dintermediate
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'related_query_name' with 'related_name'
Using 'intermediate' which is not a Django attribute
5fill in blank
hard

Fill all three blanks to define a many-to-many field with a through model, related name, and symmetrical set to False.

Django
class Author(models.Model):
    name = models.CharField(max_length=100)
    books = models.ManyToManyField(Book, [1]='Membership', [2]='authors', [3]=False)
Drag options to blanks, or click blank then click option'
Athrough
Brelated_name
Csymmetrical
Drelated_query_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'related_query_name' instead of 'related_name'
Forgetting to set symmetrical to False when needed