0Pricing
Django Academy · レッスン

モデルを変更して再度マイグレーションする

フィールドを追加し、その変更をDBに反映します

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

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

Models Evolve

Real apps change. When a feature needs a new column, you edit the model and let migrations carry the change to the database.

Step 1: Edit the Model

Start by adding the field in your model class. Here you give a Post a published flag with a sensible default.

class Post(models.Model):
    title = models.CharField(max_length=200)
    published = models.BooleanField(default=False)

Step 2: makemigrations

Run makemigrations. Django diffs the new model against the last one and writes a fresh migration for the added field.

python manage.py makemigrations

Step 3: migrate

Run migrate to apply the new file. The column now exists in the real table, ready to use.

python manage.py migrate

Defaults Fill Old Rows

Adding a non-nullable field to a populated table needs a value for existing rows. A default fills them in cleanly.

No Default? Django Asks

Without a default, makemigrations stops and prompts you to provide a one-off value for existing rows before continuing.

Each Change Is a New File

Django never edits old migrations. Every model change becomes a new numbered file stacked on top of the last.

Renaming a Field

Rename a field and Django may ask if you renamed rather than dropped-and-added. Answer yes to keep the column data.

Removing a Field

Delete a field from the model, then makemigrations writes a RemoveField step. Apply it and the column is gone for good.

Reversing a Migration

To undo, migrate back to an earlier number. Django runs the migrations in reverse to restore the previous schema.

python manage.py migrate blog 0001

Commit the Files

Always commit the new migration with your model edits. Teammates run migrate and get the same schema, no surprises.

Quick Check

You add a required field to a table that already has rows. What does Django need?

Recap

The loop is simple: edit the model, makemigrations, migrate, commit. Each change becomes a new file, so your history stays honest. ✅

よくある質問

「モデルを変更して再度マイグレーションする」レッスンは無料ですか?

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

「モデルを変更して再度マイグレーションする」で何を学びますか?

フィールドを追加し、その変更をDBに反映します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「モデルを変更して再度マイグレーションする」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. makemigrationsとmigrateの違い
  2. マイグレーションファイルを読む
  3. モデルを変更して再度マイグレーションする
  4. sqlmigrateでSQLを調べる
← Django Academyに戻る