On: April 22, 2010 By: Chris Robison In: Django
I always seem to forget this command and have to search the Django documentation to find it. Here's the command:
python manage.py dumpdata --indent=4 > initial_data.json
This will give you a nice .json file full of data from your database that looks similar to this one from the documentation. You can specify a different file type like yaml or xml by add "--format=xml" as an argument. The "--indent=4" makes the .json file easy on the eyes. You can also specify a specific app or apps to dump data for by adding the app name or names after dumpdata command. The way I have the command written will dump all data from all apps.
Now, if you delete your database and do a syncdb, the data from the .json file (or whatever type you chose) will be entered into the database right after the table creation. This is useful for development if you are like me, and just delete the database and run syncdb to save time. It is also useful when moving to a different database or to a production environment when you may need to preload the database with info.
Posted in: Django