0
0
Djangoframework~5 mins

Model relationships preview in Django - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a ForeignKey in Django models?
A ForeignKey creates a many-to-one relationship between two models. It means each record in the model with the ForeignKey points to one record in another model, like a child pointing to a parent.
Click to reveal answer
beginner
Explain ManyToManyField in Django models.
ManyToManyField creates a many-to-many relationship. It means records in one model can relate to many records in another model, and vice versa, like friends who can have many friends.
Click to reveal answer
beginner
What does OneToOneField represent in Django?
OneToOneField creates a one-to-one relationship between two models. Each record in one model matches exactly one record in another, like a person and their unique passport.
Click to reveal answer
intermediate
How does Django handle reverse relationships automatically?
Django automatically creates reverse relationships so you can access related objects from either side. For example, from a parent you can get all children without extra code.
Click to reveal answer
intermediate
Why use related_name in Django model relationships?
related_name sets the attribute name for reverse lookups. It helps avoid clashes and makes code clearer when accessing related objects from the other side.
Click to reveal answer
Which Django field type would you use to link a blog post to a single author?
AForeignKey
BManyToManyField
COneToOneField
DCharField
If you want to model students and courses where each student can take many courses and each course can have many students, which field is best?
AForeignKey
BOneToOneField
CManyToManyField
DIntegerField
What does OneToOneField ensure in Django models?
AOne record links to exactly one record
BMany records link to many records
CMultiple records link to one record
DNo relationship
What is the purpose of the related_name attribute in Django relationships?
ATo set the field's max length
BTo set the database table name
CTo define the primary key
DTo name the reverse relation attribute
How can you access all related objects from the reverse side of a ForeignKey?
AUsing the model's primary key
BUsing the related_name or default attribute
CUsing a ManyToManyField
DUsing a OneToOneField
Describe the three main types of model relationships in Django and give a simple real-life example for each.
Think about parents and children, friends, and unique pairs like person and passport.
You got /4 concepts.
    Explain how Django's related_name helps when working with model relationships and why it is useful.
    Consider how you access related objects from the other side.
    You got /4 concepts.