0
0
Djangoframework~20 mins

OneToOneField for one-to-one in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OneToOneField Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when you access a related object via OneToOneField?

Consider these Django models:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField()

If you have a User instance u, what does u.profile return?

AThe <code>Profile</code> instance linked to <code>u</code>, or raises <code>Profile.DoesNotExist</code> if none exists
BAlways returns None
CThe <code>User</code> instance itself
DA list of all <code>Profile</code> instances linked to <code>u</code>
Attempts:
2 left
💡 Hint

Think about how OneToOneField creates a direct link between two models.

📝 Syntax
intermediate
2:00remaining
Which is the correct way to define a OneToOneField in Django?

Choose the correct syntax to define a one-to-one relationship from Profile to User:

Auser = models.ManyToManyField(User)
Buser = models.ForeignKey(User, unique=True, on_delete=models.CASCADE)
Cuser = models.OneToOneField(User, on_delete=models.CASCADE)
Duser = models.OneToManyField(User)
Attempts:
2 left
💡 Hint

OneToOneField is a specific field type in Django models.

🔧 Debug
advanced
2:00remaining
Why does accessing a OneToOneField related object raise an error?

Given these models:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)

u = User.objects.create(username='alice')
print(u.profile)

This code raises Profile.DoesNotExist. Why?

ABecause <code>on_delete=models.CASCADE</code> deletes the <code>User</code> instance
BBecause <code>OneToOneField</code> requires <code>null=True</code> to work
CBecause <code>User</code> model does not have a <code>profile</code> attribute by default
DBecause no <code>Profile</code> instance linked to <code>u</code> exists yet
Attempts:
2 left
💡 Hint

Think about what happens if the related object is missing.

state_output
advanced
2:00remaining
What is the output of this Django shell code involving OneToOneField?

Given these models and code:

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.CharField(max_length=100)

u = User.objects.create(username='bob')
p = Profile.objects.create(user=u, bio='Hello')
print(u.profile.bio)

What will be printed?

Abob
BHello
CProfile object at some memory address
DRaises Profile.DoesNotExist
Attempts:
2 left
💡 Hint

Remember that u.profile accesses the related Profile instance.

🧠 Conceptual
expert
3:00remaining
Why use OneToOneField instead of ForeignKey with unique=True?

In Django, both OneToOneField and ForeignKey with unique=True can enforce one-to-one relationships. Why is OneToOneField preferred?

AOneToOneField creates a reverse attribute on the related model automatically, improving code clarity and access
BForeignKey with unique=True does not enforce uniqueness at the database level
COneToOneField allows multiple related objects, unlike ForeignKey
DForeignKey with unique=True is deprecated and no longer supported in Django
Attempts:
2 left
💡 Hint

Think about how Django creates reverse relations and the developer experience.