liveBook · Manning

AddField

As explained in Django in Action

AddField is a Django migration operation that adds a model field and its corresponding database column.

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

How Django in Action covers it

How deep it goes
Explains generated dependencies, field configuration, and existing-row defaults, but stops short of detailed migration internals.
Examples
Uses Promoter.famous_for, makemigrations output, and a generated 0002 migration file.
What it assumes
Assumes a Django model and existing migration, while teaching the generated dependency and default requirement from the change itself.
What you can do after
Operate/configure a Django schema change by declaring a field, generating its migration, supplying defaults, and applying it.
How it connects
Ties model fields to migrations, database rows, default values, and Django Admin.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.