Skip to content

Instantly share code, notes, and snippets.

@fyaconiello
Created April 19, 2012 19:26
Show Gist options
  • Save fyaconiello/2423447 to your computer and use it in GitHub Desktop.
Save fyaconiello/2423447 to your computer and use it in GitHub Desktop.
Django South Usage (for beginners)

Database Instructions

Installing south (should already be installed if you used my OSX Lion setup instructions)
  • Install South using pip
sudo pip install south
  • Add south to your INSTALLED APPS within your django project's settings.py file
INSTALLED_APPS = (
  'south',
  ...
)
Is this a brand new project?
  • Yes? then sync your DB up the django way. this is going to get any django core apps that are not utilizing migrations up and running (such as auth, and south_migrations)
python manage.py syncdb
  • No? then you have probably already created some apps that have models that have already been created via syncdb... which means we need to fake out your database (this isn't as hacky as it sounds).
python manage.py schemamigration <appname> --initial
python manage.py migrate <appname> --fake
Making a new south tracked application
  • Create an app:
python manage.py startapp example
  • Add a model to <PROJECT>/example/models.py
from django.db import models
from django.utils.translation import ugettext as _

class Foo(models.Model):
	"""
	Foo Model
	"""
	title = models.CharField(
		verbose_name  = _(u'Title'), 
		help_text     = _(Foo\'s title.'),
		max_length    = 255
	)
	slug = models.SlugField(
		verbose_name  = _(u'Slug'),
		help_text     = _(u'Unique identifier for this foo.'),
		max_length    = 255,
		unique        = True
	)

	class Meta:
		verbose_name_plural = "Foos"
		verbose_name        = "Foo"
		ordering            = ['title', ]
	
	def __unicode__(self):
		return self.title
  • IMPORTANT: DO NOT RUN syncdb, we want the models in example app to be tracked via south.
  • Instead, create the initial migration.
python manage.py schemamigration example --initial
  • Run your initial migration
python manage.py migrate example
  • Make another change to your models file (you probably won't get everything you want in each of your models correct on the first pass.)
python manage.py schemamigration example --auto
  • Apply the latest migration file
python manage.py migrate example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment