If you work with multiple environments and have separate databases you probably want to synchronize your databases at some point.
Django automatically creates migrations if the scheme changes and you can manually create data migrations. However, if you do changes in the admin panel, those migration files are not generated automatically.
In the following, I will explain the DB sync process for these data changes in detail. Make sure to read carefully since it involves critical steps.
1. Export Data from Django Database
In order to do so, you can pull all the data from the database into a JSON file with the dumpdata
command (see docs).
Attention: Be careful with the exports of all data, also sensitive information like sessions or user data including passwords will be exported. To prevent the export of those you can add the following flags to the dumpdata
command:
By default, all the data will be exported in a single line. Therefore it is recommended to pretty-print the JSON file in most cases. This way it is human-readable and easier to double-check.
The following flag will add new lines and indentions to the output file:
2. Import Data into Django Database
To update the database of a different environment with your dumped data you can use the loaddata
command (see docs). This commands loads everything from the JSON file into the database. If the data is already in the database, meaning the primary key is the same, the data will be overwritten with the data from the JSON file. In case the data is in the database but not in the JSON file, the data will remain untouched.
Do you want to ensure that only the JSON file data is in the DB? Then you have to flush the database beforehand:
3. Conclusion
These commands will export the data from one database and then import the data from the exported file to another database:
If you wish to automate this task, you can make use of Git Hooks.
Happy Coding! ✌️