0Pricing
Django Academy · レッスン

ForeignKeyとon_delete

一対多のモデル関連と削除時の動作を定義します

「ForeignKeyとon_delete」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「ForeignKeyとon_delete」レッスンは無料ですか?

はい。「ForeignKeyとon_delete」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。

「ForeignKeyとon_delete」で何を学びますか?

一対多のモデル関連と削除時の動作を定義します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Django Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「ForeignKeyとon_delete」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDjango Academyレッスンでコードを書いて実行できますか?

はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ForeignKeyとon_delete
  2. ManyToManyFieldとThrough Models
  3. Profiles用のOneToOneField
  4. related_nameと逆引き
← Django Academyに戻る