0
0
Djangoframework~10 mins

Model relationships preview in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Model relationships preview
Define Models
Add Relationship Fields
Create Instances
Link Instances via Relationships
Query Related Data
Access Related Objects
This flow shows how Django models are defined with relationships, instances are created and linked, and related data is accessed.
Execution Sample
Django
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)
Defines two models: Author and Book, where each Book links to one Author using a ForeignKey.
Execution Table
StepActionEvaluationResult
1Define Author modelAuthor with name fieldModel ready
2Define Book model with ForeignKey to AuthorBook with title and author linkModel ready
3Create Author instance: name='Alice'Author object createdAuthor(id=1, name='Alice')
4Create Book instance: title='Django Basics', author=Author(id=1)Book object createdBook(id=1, title='Django Basics', author=1)
5Query Book's authorAccess book.authorAuthor(id=1, name='Alice')
6Query Author's booksAccess author.book_set.all()QuerySet with Book(id=1)
7ExitAll relationships linked and accessibleEnd of preview
💡 All model relationships created and linked; queries return related objects correctly.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
Author instanceNoneAuthor(id=1, name='Alice')Author(id=1, name='Alice')Author(id=1, name='Alice')
Book instanceNoneNoneBook(id=1, title='Django Basics', author=1)Book(id=1, title='Django Basics', author=1)
Key Moments - 2 Insights
Why does book.author return an Author object, not just an ID?
Because Django's ForeignKey creates a link to the related Author object, so accessing book.author returns the full Author instance, as shown in step 5.
How can we get all books written by an author?
Using author.book_set.all() Django automatically creates a reverse relation named book_set, shown in step 6, to access all related Book objects.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the author of the Book instance at step 4?
ANone
BAuthor(id=1, name='Alice')
CBook(id=1, title='Django Basics')
DAuthor(id=2, name='Bob')
💡 Hint
Check step 4 in the execution table where the Book instance is created with author=Author(id=1).
At which step do we first create an Author instance?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the action column in the execution table for when the Author object is created.
If we add a second Book linked to the same Author, how would author.book_set.all() change?
AIt would include both Book instances
BIt would only show the first Book
CIt would show no books
DIt would cause an error
💡 Hint
Refer to the explanation in key moments about author.book_set.all() returning all related books.
Concept Snapshot
Django model relationships use fields like ForeignKey to link models.
Create instances and assign related objects.
Access related data via attributes (e.g., book.author) or reverse sets (author.book_set).
This connects data like real-life linked records.
Use on_delete to control deletion behavior.
Full Transcript
In Django, models can be linked using relationships like ForeignKey. First, you define models with fields and relationship fields. Then you create instances of these models. For example, an Author instance and a Book instance linked to that Author. When you access book.author, Django returns the linked Author object, not just an ID. Similarly, you can get all books by an author using author.book_set.all(). This preview shows how models relate and how to access related data step-by-step.