Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django workflow when modifying models frequently?

Tags:

as I usually don't do the up front design of my models in Django projects I end up modifying the models a lot and thus deleting my test database every time (because "syncdb" won't ever alter the tables automatically for you). Below lies my workflow and I'd like to hear about yours. Any thoughts welcome..

  1. Modify the model.
  2. Delete the test database. (always a simple sqlite database for me.)
  3. Run "syncdb".
  4. Generate some test data via code.
  5. goto 1.

A secondary question regarding this.. In case your workflow is like above, how do you execute the 4. step? Do you generate the test data manually or is there a proper hook point in Django apps where you can inject the test-data-generating-code at server startup?\

TIA.

like image 327
utku_karatas Avatar asked Jan 30 '09 23:01

utku_karatas


2 Answers

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

like image 142
Harper Shelby Avatar answered Sep 20 '22 13:09

Harper Shelby


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 (call it "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

like image 43
Matthew Christensen Avatar answered Sep 20 '22 13:09

Matthew Christensen