0
0
Djangoframework~10 mins

OneToOneField for one-to-one 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 one-to-one relationship in Django models.

Django
class Profile(models.Model):
    user = models.[1](User, on_delete=models.CASCADE)
Drag options to blanks, or click blank then click option'
AForeignKey
BOneToOneField
CManyToManyField
DCharField
Attempts:
3 left
💡 Hint
Common Mistakes
Using ForeignKey instead of OneToOneField causes many profiles per user.
Using ManyToManyField allows multiple users per profile.
2fill in blank
medium

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

Django
user = models.OneToOneField(User, 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 causes errors.
Using DO_NOTHING can leave orphaned profiles.
3fill in blank
hard

Fix the error in the model field to ensure the one-to-one link is unique.

Django
user = models.OneToOneField(User, on_delete=models.CASCADE, [1]=True)
Drag options to blanks, or click blank then click option'
Anull
Bblank
Cunique
Drelated_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'null=True' does not enforce uniqueness.
Using 'blank=True' affects forms, not database uniqueness.
4fill in blank
hard

Fill both blanks to define a one-to-one field with a custom related name and allow null values.

Django
user = models.OneToOneField(User, on_delete=models.CASCADE, [1]=True, [2]='profile')
Drag options to blanks, or click blank then click option'
Anull
Bblank
Crelated_name
Dunique
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'blank' with 'null' for database nullability.
Not setting 'related_name' causes default reverse names.
5fill in blank
hard

Fill all three blanks to create a one-to-one field with cascade delete, a custom related name, and allow blank values in forms.

Django
user = models.OneToOneField(User, on_delete=models.[1], related_name='[2]', [3]=True)
Drag options to blanks, or click blank then click option'
ACASCADE
Bprofile
Cblank
DPROTECT
Attempts:
3 left
💡 Hint
Common Mistakes
Using PROTECT instead of CASCADE changes delete behavior.
Confusing 'blank' with 'null' for form vs database.