South will handle schema migrations for you:
http://south.aeracode.org/
This tutorial is a good place to start and covers the basic set-up:
http://south.aeracode.org/docs/tutorial/part1.htmlBasically, you want to revert your changes so your model matches your database, install south and configure your app to use it, then convert your models to use south by typing:
python manage.py schemamigration [appname] --initial
This creates migration files which are stored in [appname]/migrations/
You can then apply these migrations using this command:
python manage.py migrate [appname]
Note that you need to do this for each app in your project. When you change the model and want to update the database,
you can create migration files automatically with:
python manage.py schemamigration [appname] --auto
Then just apply them as you did with the initial migration.
Some best practices for avoiding issues with South:
- If your model has a NOT NULL field then it should always have
a default. That way South will know what value to fill in when
it creates the field.
- If you are changing a pre-existing model field drastically and don't care about losing data, I find it most convenient to remove the
field, migrate the schema, then add the field again with the new specification . This way
you will avoid conflicts with any existing constraints. Note that to preserve the data you'll need to create a (more complicated) data migration.
Hope this helps.