0Pricing
Django Academy · Lección

Cambiar un modelo y volver a migrar

Añada un campo e incorpore el cambio a la base de datos

Cambiar un modelo y volver a migrar es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Django Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Django Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «Cambiar un modelo y volver a migrar» es gratis?

Sí — el texto completo de «Cambiar un modelo y volver a migrar» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Django Academy, actualiza a CoddyKit PRO. El curso de Django Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Cambiar un modelo y volver a migrar»?

Añada un campo e incorpore el cambio a la base de datos Practicas Django Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Django Academy?

No se requiere experiencia previa. Django Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.

¿Cuánto tiempo toma la lección «Cambiar un modelo y volver a migrar»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Django Academy?

Sí. Cada lección de Django Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. makemigrations frente a migrate
  2. Leer un archivo de migración
  3. Cambiar un modelo y volver a migrar
  4. sqlmigrate e inspección de SQL
← Volver a Django Academy