About Reverse Data Migrations

Set the reverse data migration code to migrations.RunPython.noop if you want to be able to smoothly migrate forward and backward:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# content/migrations/0002_migrate_data.py
from django.db import migrations

def migrate_data(apps, schema_editor):
    ...

class Migration(migrations.Migration):
    dependencies = [
        ("content", "0001_initial"),
    ]
    operations = [
        migrations.RunPython(
            code=migrate_data,
            reverse_code=migrations.RunPython.noop,
        ),
    ]

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2