Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
The following is a simple django custom management command to handle model field additions and deletions on a development machine. It automates a common pattern in django development – that of renaming an existing table, recreating via syncdb, and inserti

Simple django management command to automate the process of renaming an existing table, recreating via syncdb, and inserting the old data into the new table. Currently does not handle field renames - will eat your data if you try.

To install, save this file into an installed django app's management/commands/ directory, eg


Run the command from the command line as follows

python simplemigration APPNAME.MODELNAME
from import BaseCommand, CommandError
from django.db import models
from import call_command
from django.db import connection, transaction
class Command(BaseCommand):
def handle(self, *model_labels, **options):
for label in model_labels:
m = models.get_model(*label.split('.'))
temp_name = '_' + m._meta.db_table
# rename table, then recreate
cursor = connection.cursor()
sql = 'ALTER TABLE %s RENAME TO %s' % (m._meta.db_table, temp_name)
print sql
cursor = connection.cursor()
old_fields = [f[0] for f in connection.introspection.get_table_description(cursor, temp_name)]
new_fields = [f[0] for f in connection.introspection.get_table_description(cursor, m._meta.db_table)]
insert_fields = [f if f in old_fields else '\'\'' for f in new_fields]
# insert data from temp table into new
sql = 'INSERT INTO %s SELECT %s FROM %s' % (m._meta.db_table, ', '.join(insert_fields), temp_name)
print sql
# drop temporary table
sql = 'DROP TABLE %s' % (temp_name)
print sql
except Exception, e:
print 'Last SQL statement: ' + sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment