0
0
Djangoframework~10 mins

ForeignKey for one-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 ForeignKey field in a Django model.

Django
class Comment(models.Model):
    post = models.[1]('Post', on_delete=models.CASCADE)
    content = models.TextField()
Drag options to blanks, or click blank then click option'
AManyToManyField
BCharField
CForeignKey
DOneToOneField
Attempts:
3 left
💡 Hint
Common Mistakes
Using CharField instead of ForeignKey for relationships.
Using ManyToManyField when only one post per comment is needed.
2fill in blank
medium

Complete the code to specify the behavior when the related object is deleted.

Django
post = models.ForeignKey('Post', on_delete=models.[1])
Drag options to blanks, or click blank then click option'
ACASCADE
BSET_NULL
CPROTECT
DDO_NOTHING
Attempts:
3 left
💡 Hint
Common Mistakes
Using SET_NULL without allowing null values.
Choosing PROTECT which prevents deletion.
3fill in blank
hard

Fix the error in the ForeignKey declaration to allow null values.

Django
post = models.ForeignKey('Post', on_delete=models.CASCADE, [1]=True)
Drag options to blanks, or click blank then click option'
Adefault
Bnull
Cblank
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Using blank=True only affects form validation, not database nullability.
Using default without a valid default value.
4fill in blank
hard

Fill both blanks to define a ForeignKey with a related name and allow null values.

Django
post = models.ForeignKey('Post', on_delete=models.CASCADE, [1]=True, related_name='[2]')
Drag options to blanks, or click blank then click option'
Anull
Bcomments
Cblank
Dposts
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing related_name with the model name.
Forgetting to allow null if needed.
5fill in blank
hard

Fill all three blanks to create a ForeignKey with CASCADE delete, allow blank input, and set a related name.

Django
post = models.ForeignKey('Post', on_delete=models.[1], blank=[2], related_name='[3]')
Drag options to blanks, or click blank then click option'
APROTECT
BTrue
Ccomments
DCASCADE
Attempts:
3 left
💡 Hint
Common Mistakes
Using PROTECT instead of CASCADE for on_delete.
Confusing blank=True with null=True.
Incorrect related_name causing reverse lookup errors.