AddField is a Django migration operation that adds a model field and its corresponding database column. Changing Promoter to include famous_for creates a second migration dependent on 0001_initial. section 10.1.2
A typical AddField change involves:
- Declare the new field on the model, such as a CharField named famous_for. section 10.1.2
- Run makemigrations; for a non-nullable field, provide a value for existing rows. section 10.1.2
- Run the migration command to apply the structural database change. section 10.1.2
Listing 10.5 The migration file for adding the famous_for field
# RiffMates/promoters/migrations/0002_promoter_famous_for.py
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("promoters", "0001_initial"), #1
]
operations = [
migrations.AddField( #2
model_name="promoter",
name="famous_for",
field=models.CharField(default="", max_length=50),
preserve_default=False,
),
]
The generated migration records its dependency, model name, field name, field definition, default value, and default-preservation behavior. section 10.1.2
