ForeignKey and on_delete
Model one-to-many links and deletion behavior.
ForeignKey and on_delete is a free Django Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Linking Two Tables
Real data connects: a book has an author, an order has a customer. A ForeignKey is how one model points to another.
One-to-Many at a Glance
A ForeignKey models a one-to-many link. One author can have many books, but each book belongs to exactly one author.
Declaring a ForeignKey
Add a field of type ForeignKey and point it at the related model. Django stores the link as an id column behind the scenes.
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)The _id Column
In the database, an author ForeignKey becomes an author_id column holding the other row's primary key. That number is the actual link.
on_delete Is Required
Every ForeignKey needs on_delete. It tells Django what to do with this row when the row it points to is deleted.
CASCADE Deletes Children
CASCADE deletes this row too when its parent goes. Remove an author and all their books vanish with them.
author = models.ForeignKey(Author, on_delete=models.CASCADE)PROTECT Blocks Deletion
Use PROTECT to refuse the delete while related rows exist. Django raises an error instead of removing the parent.
author = models.ForeignKey(Author, on_delete=models.PROTECT)SET_NULL Keeps the Child
SET_NULL keeps the row but clears the link, setting the column to NULL. It needs null=True on the field to work.
author = models.ForeignKey(Author, null=True, on_delete=models.SET_NULL)SET_DEFAULT and DO_NOTHING
SET_DEFAULT swaps in the field's default, while DO_NOTHING leaves the database to decide. Reach for these only in special cases.
Accessing the Related Object
From a book instance you read the linked author straight through the field. Django fetches that row for you on demand.
book = Book.objects.first()
print(book.author.name)Filtering Across the Link
Span the relationship in queries with the double underscore. Reach into the related model's fields directly from filter.
Book.objects.filter(author__name="Ada")Quick Check
You delete an author but want their books to stay, just unlinked. Which on_delete value fits?
Recap
Nice work! A ForeignKey models one-to-many links, and on_delete decides each row's fate when its parent is removed. 🔗
Frequently asked questions
Is the “ForeignKey and on_delete” lesson free?
Yes — the full text of “ForeignKey and on_delete” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “ForeignKey and on_delete”?
Model one-to-many links and deletion behavior. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “ForeignKey and on_delete” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- ForeignKey and on_delete
- ManyToManyField and Through Models
- OneToOneField for Profiles
- related_name and Reverse Lookups