Django Academy · Ders

ForeignKey ve on_delete

Bire çok bağlantıları ve silme davranışını modelleyin.

1. ders / 413 adım

ForeignKey ve on_delete, CoddyKit'te ücretsiz bir Django Academy dersidir. Bu, 4 dersinin 1. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Django Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Django Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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. 🔗

Başlamak ücretsiz

Yapay zeka eğitmeniyle Python öğren — ücretsiz

Tarayıcında gerçek kod yaz ve çalıştır, 7/24 yapay zeka eğitmeninden anında yardım al; web'de ya da uygulamada kaldığın yerden devam et.

Kurslar
30
Dersler
120

Sıkça Sorulan Sorular

“ForeignKey ve on_delete” dersi ücretsiz mi?

Evet — “ForeignKey ve on_delete” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Django Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Django Academy kursu toplamda 4 dersten oluşur.

“ForeignKey ve on_delete” dersinde ne öğreneceğim?

Bire çok bağlantıları ve silme davranışını modelleyin. Django Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Django Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Django Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 1. dersidir.

“ForeignKey ve on_delete” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Django Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Django Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. ForeignKey ve on_delete
  2. ManyToManyField ve Through Modelleri
  3. Profiller için OneToOneField
  4. related_name ve Tersine Aramalar
← Django Academy Sayfasına Dön