Migrating Django Model Changes – South

Something I learnt after installing and using django to create my first model was that

python manage.py syncdb

Does not propagate changes made to an existing model to the database. To make such changes like addition of a new field to a model would require that I drop the database table first and then syncdb the new changes.

Introducing South

South does exactly that. It manages the changes in django models in what it calls migrations, allowing you to roll forward and back the changes to a django model in the database.

Usage

  1. Installation and Initiation within a Django Project:
    Installation makes South available to all Django projects

    pip install South
    

    Next add ‘south’ to the list of INSTALLED_APPS in settings.py of the Django project.

    Check if Django detects south

    python manage.py shell
    import south
    

    If the import doesn’t work check if the INSTALLED_APP setting is not overridden somewhere else,
    if the import works fine run syncdb to initialize south, which creates a table in the database to keep track of migrations.

    python manage.py syncdb
    
  2. Using south to initiate and propagate model changes:
    Using South with a Django app is a two step process. Initialize the first migration of a model and later Auto migrate any new changes.

    1. Using South for a new App (one which has not been synced to the database yet.)
      Initialize the first migration of the app:

      python manage.py schemamigration django_app --initial
      python manage.py migrate django_app
      

      After initialization to propagate all future changes in the django model to the database enter

      python manage.py schemamigration django_app --auto
      python manage.py migrate django_app
      
    2. Using South for an existing App (one which has been synced to the database earlier using syncdb)
      First convert the app to use South:

      python manage.py convert_to_south django_app
      python manage.py migrate django_app
      

      The same commands as in the earlier case now apply to propagate all future changes in the django model to the database

      python manage.py schemamigration django_app --auto
      python manage.py migrate django_app
      
  3. Troubleshooting South
    For whatsoever reason if South fails to migrate the changes, resetting it is easy. Delete the “migrations” folder from within the Django app directory and delete the app entries from within the database table “south_migrationhistory”. Now convert the app to use South again. Only thing to consider is that the model definition should match the database table when you make this conversion.

Using Postgresql with Django

On installing Django and following the tutorial I decided to use Postgresql instead of  SQLite3 as the backend.

I made the following changes in the settings.py file to enable Postgresql as the database backend:

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'database_name', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'database_user',
'PASSWORD':'database_user_password',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}

Now on running

python manage.py syncdb

I got the following error:

ImportError:  No module named psycopg2.extensions

Which would imply that I need to install psycopg2 for my installation of python.

I tried to pip install and easy_install the same but received the following error:

error: Unable to find vcvarsall.bat

Googling the problem lead me to understand that I am required to download the psycopg2.exe file for windows from stickpeople and then use 7zip to extract 2 items from the .exe file -the folder psycopg2 and the psycopg2.egg-info file. Place these 2 items in the python/Lib folder (in case your using virtual env then it would be within the Lib folder of the Virtual Env folder)

Run

python manage.py syncdb

It works..

Reference:
http://samudranb.com/2012/06/02/how-to-setup-a-djangopython-development-env-on-windows/
http://www.stickpeople.com/projects/python/win-psycopg/