Django workflow when modifying models frequently? Django workflow when modifying models frequently? python python

Django workflow when modifying models frequently?


Steps 2 & 3 can be done in one step:

manage.py reset appname

Step 4 is most easily managed, from my understanding, by using fixtures


This is a job for Django's fixtures. They are convenient because they are database independent and the test harness (and manage.py) have built-in support for them.

To use them:

  1. Set up your data in your app (callit "foo") using the admin tool
  2. Create a fixtures directory in your"foo" app directory
  3. Type: python manage.py dumpdata --indent=4 foo > foo/fixtures/foo.json

Now, after your syncdb stage, you just type:

 python manage.py loaddata foo.json

And your data will be re-created.

If you want them in a test case:

class FooTests(TestCase):    fixtures = ['foo.json']

Note that you will have to recreate or manually update your fixtures if your schema changes drastically.

You can read more about fixtures in the django docs for Fixture Loading


Here's what we do.

  1. Apps are named with a Schema version number. appa_2, appb_1, etc.

  2. Minor changes don't change the number.

  3. Major changes increment the number. Syncdb works. And a "data migration" script can be written.

    def migrate_appa_2_to_3():    for a in appa_2.SomeThing.objects.all():        appa_3.AnotherThing.create( a.this, a.that )        appa_3.NewThing.create( a.another, a.yetAnother )    for b in ...

The point is that drop and recreate isn't always appropriate. It's sometimes helpful to move data form the old model to the new model without rebuilding from scratch.