[Answered ]-Django migrations — how do I wipe a model as part of a migration?

1👍

Couldn’t figure out The Right Way to do it, but managed to hack it by:

  • Start with a clean git history (no dirty files) — makes the next steps easier
  • Rename the model in question to something else, like Model2
  • Run makemigrations
  • Revert all changes except the generated migration
  • Reorder the generated migration file so DeleteModel comes before CreateModel, and change the name of the model being created in CreateModel back to the original model name. It should look like this:
class Migration(migrations.Migration):

    dependencies = [
        ("<app name>", "<previous migration>"),
    ]

    operations = [
        migrations.DeleteModel(
            name="ModelName",
        ),
        migrations.CreateModel(
            name="ModelName",
            fields=[
                # < all fields of your model >
            ],
            options={
                # < all meta options of your model >
            },
        ),
    ]
👤vgel

Leave a comment