Django’s manage.py script comes with some great utilitarian commands. Two of my favorites are dumpdata and loaddata. I’ve used these commands recently to migrate a Django app’s data to the same Django app now running on Heroku. Here’s a little taste of the glory.
By default it comes out as json, but you can one-off processes such as the loaddata command. But the next hurdle will be getting your data.json onto the Heroku server. This is always handled in a git push. And to make it a little bit more interesting, let’s say that the code you have isn’t particularly sensitive, but the data is. So, we’re going to commit the data.json, push it, remove the latest git commit, be able to push our code to the origin server, and still run loaddata on Heroku. Sheesh.
Ok, so commit your file:
Push it to Heroku:
Remove file from staging area:
Save your code w/o data included:
And now you should still have data.json on your Heroku server and be able to run loaddata:
If it all works, your data fixtures should be found and you should see a confirmation message that they were loaded into the db. Just like a dream. The only thing that would make it better is if you never had to commit data.json at all. Too bad Heroku couldn’t pick up local file input like it can pick up STDIN in a ‘heroku run’ command!
Update: Force Next Push
Also note that if you continue to commit to git locally on future enhancements to your app and then want to push again to Heroku, you’ll likely get an error message:
1 2 3 4 5 6 7
To get around this, next time you push to Heroku, try:
And force it to do your bidding.