0
0
Djangoframework~5 mins

OneToOneField for one-to-one in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a OneToOneField in Django?
A OneToOneField creates a one-to-one relationship between two models, meaning each record in one model is linked to exactly one record in another model.
Click to reveal answer
beginner
How do you define a one-to-one relationship in Django models?
Use models.OneToOneField inside a model to link it to another model with a one-to-one relationship.
Click to reveal answer
beginner
What is a real-life example of a one-to-one relationship?
A person and their passport: each person has exactly one passport, and each passport belongs to exactly one person.
Click to reveal answer
intermediate
What happens if you try to create two records linked to the same record using a OneToOneField?
Django will raise an IntegrityError because OneToOneField enforces uniqueness, so only one record can link to the other.
Click to reveal answer
intermediate
How do you access the related object in a one-to-one relationship?
You can access the related object using the attribute name defined by the OneToOneField on the model instance.
Click to reveal answer
What does OneToOneField enforce between two Django models?
ANo relationship between models
BMultiple records in one model can link to one record in the other
COne record links to many records in the other model
DEach record in one model links to exactly one record in the other model
Which Django field type would you use to create a one-to-one relationship?
Amodels.ManyToManyField
Bmodels.ForeignKey
Cmodels.OneToOneField
Dmodels.CharField
If a OneToOneField is set on model A pointing to model B, how do you access model B from an instance of model A?
AUsing the attribute name of the <code>OneToOneField</code>
BUsing a query on model B
CUsing a list of related objects
DYou cannot access model B from model A
What error occurs if you try to link two records to the same record using a OneToOneField?
AValueError
BIntegrityError
CTypeError
DNo error
Which of these is a good example of a one-to-one relationship?
AA student and their ID card
BA book and its author
CA teacher and their students
DA blog post and its comments
Explain how to create and use a OneToOneField in Django models.
Think about how two models can be connected so each record matches exactly one record in the other.
You got /4 concepts.
    Describe a real-life situation where a one-to-one relationship fits well and how it maps to Django models.
    Think about unique pairs in everyday life.
    You got /4 concepts.