Nov 23, 2005, 12:12:47 PM11/23/05
There is a thread in django-developers list talking about the way Django
and Rails handle frequent changes to models on early stages of
development ('prototyping' - to sound cool). Neither framwork does this
transparently which is understandable because it's definitely hard and
even arguably desirable at all :-).
I'd like to share my method of doing this task where I use one
undocumented feature that Adrian Holovaty once mentioned in this list.
I'm writing here because I'm not sure if it's ready for the Cookbook in
the wiki. May be everyone will reply that I'm doing it incredibly
painful way and should just read this URL and that URL to do it right
:-). Also feel free to correct my English since it's not my native
language. So to the point.
1. I begin designing a model to the whole depths of my current domain
knowledge. But I'm absolutely sure that in a near future my knowledge
will change and will require changing models.
2. Significant changes to models that require database changes are:
adding/removing/renaming fields, adding/removing/renaming tables,
changing types and sizes of fields. But there is no point of doing
something with database when adding some custom method or changing META
3. When database change is required I use:
django-admin.py sqlreset <appname>
... which effectvely wipes out entire application structure then creates
4. But this also wipes all the test data in your database and if you had
to recreate it by hand each time it would defeat the whole point of this
method :-). Luckily you can easily automate it. After creating the
structure 'sqlreset' tries to locate and execute SQL script in
This script should contain SQL statements that create test data for your
Creating this file from scratch by hand is boring. Instead I create all
the data using shiny Django's admin interface and then use database
tools to generate such script. For PostgreSQL this utility called
pg_dump and can be used like this:
pg_dump -a -D <db_name> > <appname>.sql
-a: dump only data, not structure (because structure will be created by
-D: use INSERT statements with column names
Then I edit the resulting file removing all core data leaving only
INSERTS into my applications tables.
It still sounds a bit complex but I've done it only first time. On all
subsequent changes it's easier to correct this file by hand. And often
it's not even required when you, for example, just change the length of
a text field.
6. The 'sqlreset' outputs all SQL statements to console. To further
automate the task I redirect this output into command-line database
client instead of copy-pasting it by hand:
django-admin.py sqlreset <appname> | psql <db_name>