How to preserve your existing table data when adding new fields to a Django model, using an export-reset-import workflow.
Export > Reset > Import
Most of the time, Django will find errors after you’ll change the model, so the problem is, you can’t export the data after you’ve changed your model. you need to export it. edit your model and then reset+import.
use those bash scripts:
Export
`DB=/var/www/django/db APP=myapp
echo “Exporting database…” &&\ python manage.py dumpdata $APP –format=‘json’ –indent=4 –verbosity=1 > backup/DUMP.json &&\ echo ‘Backing up db file…’ &&\ cp $DB $DB.bk`
Reset & Import
Now, update your model with the new fields and run the following:
DB=/var/www/django/db APP=myapp echo 'Rebuilding db...' &&\\ python manage.py reset $APP &&\\ python manage.py loaddata backup/DUMP.json
