ForeignKey und on_delete
Eins-zu-viele-Beziehungen und Löschverhalten modellieren
ForeignKey und on_delete ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🔗
Häufig gestellte Fragen
Ist die Lektion „ForeignKey und on_delete“ kostenlos?
Ja — der vollständige Text von „ForeignKey und on_delete“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „ForeignKey und on_delete“?
Eins-zu-viele-Beziehungen und Löschverhalten modellieren Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „ForeignKey und on_delete“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- ForeignKey und on_delete
- ManyToManyField und Through-Modelle
- OneToOneField für Profile
- related_name und umgekehrte Lookups