Use Django Fixtures to Automatically Load Data when You Install an App
First, load some data via the admin that should always be there when someone installs the app.
Next, dump that data out into JSON format into a fixture:
./manage.py dumpdata [app_name] > [app_name]/fixtures/initial_data.json
Now whenever someone installs the app and runs syncdb, they’ll get that initial data loaded into their database.
Comments
Something about this setup makes me think it could be used to do some basic data migration.
models.py.It’s not versioned like a proper migration, but it would be better than the default option of creating new fields manually.
I second the motion for the magical script. Especially one that would reload data from all available fixtures or at least give the ability to specify more than one fixture. Good call.
Test of Trey’s WMD
This is a pretty cool trick.
What…another paragraph.
Haven’t looked into this too deeply but my common process for updating a model is basically the same thing:
$ python manage.py dumpdata [app] > dump.json$ python manage.py reset [app]$ python manage.py loaddata dump.jsonI thought about trying to automate this because it happens fairly regularly with new apps, but I haven’t devoted time to it because in order to use
dumpdatathe model has to match the database or it will throw errors.When I get ahead of myself and save before I
dumpdataI have to go back and comment out the new field or revert the changed field temporarily.I think this definitely warrants some good time and effort.
one more thing about my method, my file dump.json… at any given moment I have no idea what is in it, I always overwrite the same file as I am about to reset and load it.
What do you think about that?