0
0
Djangoframework~10 mins

Through model for extra fields on M2M 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 using a through model.

Django
class Book(models.Model):
    authors = models.ManyToManyField(Author, through=[1])
Drag options to blanks, or click blank then click option'
ABook
BBookAuthor
CAuthor
DAuthorBook
Attempts:
3 left
💡 Hint
Common Mistakes
Using the related model name instead of the through model.
Forgetting to define the through model class.
2fill in blank
medium

Complete the through model with the correct field type for the foreign key to Book.

Django
class BookAuthor(models.Model):
    book = models.[1](Book, on_delete=models.CASCADE)
Drag options to blanks, or click blank then click option'
AForeignKey
BManyToManyField
COneToOneField
DCharField
Attempts:
3 left
💡 Hint
Common Mistakes
Using ManyToManyField inside the through model.
Using OneToOneField which restricts to one relation.
3fill in blank
hard

Fix the error in the through model by completing the field for author.

Django
class BookAuthor(models.Model):
    author = models.[1](Author, on_delete=models.CASCADE)
Drag options to blanks, or click blank then click option'
AManyToManyField
BCharField
CForeignKey
DIntegerField
Attempts:
3 left
💡 Hint
Common Mistakes
Using CharField or IntegerField instead of ForeignKey.
Using ManyToManyField inside the through model.
4fill in blank
hard

Fill both blanks to add an extra field and a string representation method in the through model.

Django
class BookAuthor(models.Model):
    book = models.ForeignKey(Book, on_delete=models.CASCADE)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)
    [1] = models.IntegerField(default=0)

    def __str__(self):
        return f"[2] by [3] with contribution [1]%"
Drag options to blanks, or click blank then click option'
Acontribution
Bself.author
Cself.book
Dcontributor
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names or missing self in __str__ method.
Confusing attribute names.
5fill in blank
hard

Fill all three blanks to create a query that filters BookAuthor entries with contribution greater than 50.

Django
high_contrib = BookAuthor.objects.filter([1]__[2]=[3])
Drag options to blanks, or click blank then click option'
Acontribution
Bgt
C50
Dauthor
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong field names or lookup types.
Using single underscore instead of double underscore.