Skip to content

Instantly share code, notes, and snippets.

@mintyPT
Last active December 16, 2024 14:35
Show Gist options
  • Save mintyPT/49f9a3d0fe1ab1f32b219ee41c96c79c to your computer and use it in GitHub Desktop.
Save mintyPT/49f9a3d0fe1ab1f32b219ee41c96c79c to your computer and use it in GitHub Desktop.
Migrate charfield's choice in django
# If you change some field's choice, django will not migrate the value at
# the database level. You need to do it.
#
# Generate an empty migration:
# > python manage.py makemigrations --empty <app_name>
#
# This will migration the value in the `state` field from `new` to `new_b`
# (and back if necessary).
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [("<app_name>", "0001_abc")]
operations = [
migrations.RunSQL(
"update <app_name>_<table> set state='new_b' where state='new';",
"update <app_name>_<table> set state='new' where state='new_b';",
)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment