Dec

7

Darwin Rears His Head

I'm using Django Evolution with this project because I knew I would need to muck with my models at some point. Ladies and gentlemen, that point has come!

In order to get categories set up I had to add a foreign key to the Category model from my BlogPost model. I've used the "./manage.py --hint" → "./manage.py --hint --execute" pattern before in other projects, but I decided to use Django Evolution's "Stored Evolutions" feature this time. It turned out to be painless, and I think i could smell rose petals and the absence of stress.

  • I ran ./manage.py --hint

  • I copied the lines between the "---------" bits into a file inside blog/evolutions. That looks like this:

from django_evolution.mutations import *
from django.db import models
 
MUTATIONS = [
AddField('BlogPost', 'category', models.ForeignKey, null=True, related_model='blog.Category')
]

I also created a __init__.py file in there.

  • I added this line to the __init__ file:

SEQUENCE = ['add_category_fk']

  • I got all my changes checked in, pushed the code up to my slice and ran ./manage.py evolve.
    Django Evolution found the evolution file, warned me that it might destroy my data or blot out the sun if I let it run, and then executed the evolution perfectly.

This is a simple case, I know, but this was still a lot easier than firing up psql. A tool that only helps with the easy 80% of the cases is still a useful tool, I think.