Alterando um Modelo e Migrando Novamente
Adicione um campo e aplique a alteração ao DB
Alterando um Modelo e Migrando Novamente é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em 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 makemigrationsStep 3: migrate
Run migrate to apply the new file. The column now exists in the real table, ready to use.
python manage.py migrateDefaults 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 0001Commit 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. ✅
Perguntas Frequentes
A aula “Alterando um Modelo e Migrando Novamente” é grátis?
Sim — o texto completo de “Alterando um Modelo e Migrando Novamente” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “Alterando um Modelo e Migrando Novamente”?
Adicione um campo e aplique a alteração ao DB Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Alterando um Modelo e Migrando Novamente”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- makemigrations vs migrate
- Lendo um Arquivo de Migração
- Alterando um Modelo e Migrando Novamente
- sqlmigrate e Inspeção de SQL