How to perform a one-off migration into Django ORM

While working on a project I haven’t yet made public, I found myself needing to migrate data into a Django project without being able to use the simple, common “Use inspectdb, then migrate to the new form with South” approach some suggest.

Here’s the simplest sequence of commands I’ve found for performing a migration to an existing app with some possibility for reusing the migration later if necessary: startproject <throwaway project>
cd <throwaway project>
python startapp <real target app name>
[repeat for all target apps]
[edit and tell it about your DB and apps]
python inspectdb >
[look at and make any adjustments necessary]
[edit for each app and move the relevant bits from]
python syncdb
python dumpdata > migrated_data.json
[...and then set up South to prevent future pain] 

Nothing fancy. Just something that saved me some hassle and might do the same for someone else. Here are the actual commands I used for clarification: startproject gbi_temp
cd gbi_temp
python startapp gbindex
python startapp icolinks
python startapp ffcms_core
python inspectdb >
vim gbindex/ icolinks/ \
python syncdb
python dumpdata > migrated_data.json
mv migrated_data.json ../ffcms/
cd ../ffcms
python loaddata migrated_data.json

CC BY-SA 4.0 How to perform a one-off migration into Django ORM by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This entry was posted in Geek Stuff. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution under the same terms as the associated post.       Also, please be aware that non-constructive comments will have their URL field erased before being approved in order to combat SEO spam.