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
Why relationships model real-world data
📖 Scenario: You are building a simple Django app to manage a library. Books have authors, and each author can write many books. This shows how relationships in Django models represent real-world connections.
🎯 Goal: Create Django models that show a one-to-many relationship between authors and books. This will help you understand how Django uses relationships to model real-world data.
📋 What You'll Learn
Create an Author model with a name field
Create a Book model with a title field
Add a foreign key in Book to link to Author
Use Django's models.ForeignKey with on_delete=models.CASCADE
💡 Why This Matters
🌍 Real World
Modeling relationships like authors and books helps organize data clearly in apps like libraries, bookstores, or publishing platforms.
💼 Career
Understanding Django model relationships is essential for backend developers building database-driven web applications.
Progress0 / 4 steps
1
Create the Author model
Create a Django model called Author with a single field name that is a models.CharField with max_length=100.
Django
Hint
Use models.CharField for text fields and set max_length to 100.
2
Create the Book model
Create a Django model called Book with a single field title that is a models.CharField with max_length=200.
Django
Hint
Use models.CharField for the book title with a max length of 200 characters.
3
Add relationship from Book to Author
In the Book model, add a field called author that is a models.ForeignKey to the Author model with on_delete=models.CASCADE.
Django
Hint
Use models.ForeignKey to link Book to Author. The on_delete=models.CASCADE means if an author is deleted, their books are deleted too.
4
Complete the models with string representation
Add a __str__ method to both Author and Book models that return the name and title fields respectively.
Django
Hint
The __str__ method helps show friendly names in Django admin and shell.
Practice
(1/5)
1. Why do Django models use relationships like ForeignKey to connect data?
easy
A. To avoid using any database tables
B. To make the database slower by adding extra links
C. To store all data in a single model without separation
D. To represent real-world connections between data clearly and efficiently
Solution
Step 1: Understand the purpose of relationships in Django models
Relationships like ForeignKey link models to represent how real-world objects relate, such as a book belonging to an author.
Step 2: Recognize the benefit of clear data connections
These links help organize data logically and make queries easier, reflecting real-world connections efficiently.
Final Answer:
To represent real-world connections between data clearly and efficiently -> Option D
Quick Check:
Relationships model real-world links [OK]
Hint: Relationships connect models like real-world links [OK]
Common Mistakes:
Thinking relationships slow down the database
Believing all data should be in one model
Confusing relationships with avoiding tables
2. Which of the following is the correct way to define a one-to-many relationship in a Django model?
easy
A. author = models.ForeignKey(Author, on_delete=models.CASCADE)
B. author = models.ManyToManyField(Author)
C. author = models.CharField(max_length=100)
D. author = models.OneToOneField(Author)
Solution
Step 1: Identify the correct field for one-to-many
In Django, ForeignKey creates a one-to-many link from one model to another.
Step 2: Check the syntax for ForeignKey
The syntax author = models.ForeignKey(Author, on_delete=models.CASCADE) correctly defines this relationship.
Final Answer:
author = models.ForeignKey(Author, on_delete=models.CASCADE) -> Option A
Quick Check:
One-to-many uses ForeignKey [OK]
Hint: One-to-many uses ForeignKey with on_delete [OK]
Common Mistakes:
Using ManyToManyField for one-to-many
Forgetting on_delete argument
Using CharField for relationships
3. Given these models:
class Author(models.Model):
name = models.CharField(max_length=100)
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
What will book.author.name return if book is a Book instance?
medium
A. The title of the book
B. The name of the author linked to the book
C. An error because author is not a string
D. The primary key of the author
Solution
Step 1: Understand the ForeignKey link
The author field in Book links to an Author instance.
Step 2: Access the name attribute of the linked Author
Using book.author.name accesses the Author's name string.
Final Answer:
The name of the author linked to the book -> Option B
Quick Check:
book.author.name returns author name [OK]
Hint: ForeignKey lets you access related model fields directly [OK]
Common Mistakes:
Thinking book.author.name returns book title
Expecting an error accessing author.name
Confusing author primary key with name
4. What is wrong with this Django model relationship?
class Comment(models.Model):
post = models.ForeignKey(Post)
text = models.TextField()
medium
A. Missing the required on_delete argument in ForeignKey
B. ForeignKey should be replaced with ManyToManyField
C. TextField cannot be used for text data
D. The model name should be plural
Solution
Step 1: Check ForeignKey syntax requirements
Since Django 2.0, ForeignKey requires the on_delete argument to specify behavior on deletion.
Step 2: Identify missing on_delete argument
The model misses on_delete=models.CASCADE or similar, causing an error.
Final Answer:
Missing the required on_delete argument in ForeignKey -> Option A
Quick Check:
ForeignKey needs on_delete argument [OK]
Hint: Always add on_delete to ForeignKey fields [OK]
Common Mistakes:
Omitting on_delete causes errors
Replacing ForeignKey with ManyToManyField incorrectly
Thinking TextField is invalid for text
5. You want to model a library system where each Book can have multiple Authors, and each Author can write multiple Books. Which Django relationship should you use to model this real-world data?
hard
A. Use a OneToOneField from Author to Book
B. Use a ForeignKey from Book to Author
C. Use a ManyToManyField on Book linking to Author
D. Use a CharField listing author names in Book
Solution
Step 1: Understand the real-world relationship
Each book can have many authors, and each author can write many books, so the relationship is many-to-many.
Step 2: Choose the correct Django field for many-to-many
Django's ManyToManyField models this relationship properly, allowing multiple links both ways.
Final Answer:
Use a ManyToManyField on Book linking to Author -> Option C
Quick Check:
Many-to-many needs ManyToManyField [OK]
Hint: Many-to-many means ManyToManyField in Django [OK]