Bird
Raised Fist0
Djangoframework~10 mins

OneToOneField for one-to-one in Django - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of Django's OneToOneField?
easy
A. To store multiple values in a single database field
B. To create a many-to-many relationship between two models
C. To allow multiple records in one model to link to a single record in another
D. To link two models so each record in one matches exactly one record in the other

Solution

  1. Step 1: Understand the relationship types in Django

    Django provides different fields for relationships: ForeignKey for many-to-one, ManyToManyField for many-to-many, and OneToOneField for one-to-one.
  2. Step 2: Identify the purpose of OneToOneField

    OneToOneField links exactly one record in one model to exactly one record in another, ensuring a unique pairing.
  3. Final Answer:

    To link two models so each record in one matches exactly one record in the other -> Option D
  4. Quick Check:

    OneToOneField = unique pair link [OK]
Hint: OneToOneField means one record matches one record only [OK]
Common Mistakes:
  • Confusing OneToOneField with ForeignKey
  • Thinking it allows multiple links per record
  • Using it to store multiple values in one field
2. Which of the following is the correct way to define a OneToOneField in a Django model?
easy
A. user = models.ForeignKey(User, on_delete=models.CASCADE)
B. user = models.OneToOneField(User)
C. user = models.OneToOneField(User, on_delete=models.CASCADE)
D. user = models.ManyToManyField(User)

Solution

  1. Step 1: Check required parameters for OneToOneField

    OneToOneField requires the related model and the on_delete argument to specify behavior on deletion.
  2. Step 2: Validate the options

    user = models.OneToOneField(User, on_delete=models.CASCADE) correctly includes both the related model and on_delete=models.CASCADE. user = models.OneToOneField(User) misses on_delete, which is mandatory.
  3. Final Answer:

    user = models.OneToOneField(User, on_delete=models.CASCADE) -> Option C
  4. Quick Check:

    OneToOneField needs on_delete [OK]
Hint: Always include on_delete with OneToOneField [OK]
Common Mistakes:
  • Omitting on_delete argument
  • Using ForeignKey or ManyToManyField instead
  • Incorrect field syntax
3. Given these models:
class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField()

user = User.objects.create(username='anna')
profile = Profile.objects.create(user=user, bio='Hello!')
print(profile.user.username)

What will be printed?
medium
A. Hello!
B. anna
C. profile
D. Error: user attribute missing

Solution

  1. Step 1: Understand the OneToOneField link

    The Profile model links to User via OneToOneField named 'user'. The profile instance has a user with username 'anna'.
  2. Step 2: Evaluate the print statement

    Accessing profile.user.username fetches the username of the linked User, which is 'anna'.
  3. Final Answer:

    anna -> Option B
  4. Quick Check:

    profile.user.username = anna [OK]
Hint: Access linked user via profile.user.username [OK]
Common Mistakes:
  • Printing profile.bio instead of username
  • Confusing attribute names
  • Assuming user attribute is missing
4. What is wrong with this model definition?
class Employee(models.Model):
    user = models.OneToOneField(User)
    department = models.CharField(max_length=100)
medium
A. Missing on_delete argument in OneToOneField
B. OneToOneField cannot link to User model
C. department field must be IntegerField
D. OneToOneField should be ForeignKey

Solution

  1. Step 1: Check OneToOneField requirements

    OneToOneField requires the on_delete argument to specify what happens if the linked User is deleted.
  2. Step 2: Analyze the model code

    The model misses on_delete, which will cause an error when running migrations or server start.
  3. Final Answer:

    Missing on_delete argument in OneToOneField -> Option A
  4. Quick Check:

    OneToOneField needs on_delete [OK]
Hint: Always add on_delete to OneToOneField [OK]
Common Mistakes:
  • Forgetting on_delete causes errors
  • Thinking OneToOneField can't link to User
  • Confusing field types for department
5. You want to extend Django's User model to add a phone number without modifying the original User table. Which is the best way to do this using OneToOneField?
hard
A. Create a new Profile model with a OneToOneField to User and add phone number there
B. Add a phone number field directly to the User model
C. Use a ForeignKey from User to Profile with phone number
D. Create a ManyToManyField between User and phone numbers

Solution

  1. Step 1: Understand extending User without changing it

    To add extra info without altering User, create a separate model linked one-to-one to User.
  2. Step 2: Choose the correct relationship

    OneToOneField in a Profile model allows storing phone number linked uniquely to each User.
  3. Final Answer:

    Create a new Profile model with a OneToOneField to User and add phone number there -> Option A
  4. Quick Check:

    Extend User via OneToOneField in separate model [OK]
Hint: Use OneToOneField in new model to extend User [OK]
Common Mistakes:
  • Modifying User model directly
  • Using ForeignKey or ManyToManyField incorrectly
  • Not linking phone number uniquely to User