Skip to content

Instantly share code, notes, and snippets.

@MarkusH
Last active September 1, 2016 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkusH/92e1d875807c12837a0416e163309be3 to your computer and use it in GitHub Desktop.
Save MarkusH/92e1d875807c12837a0416e163309be3 to your computer and use it in GitHub Desktop.
Move a model to another app

Definitions

  • old_app -- App that holds the old model
  • new_app -- App that holds the new model
  • others -- Placeholder for all apps that reference this model with a FK, O2O, M2M field or by inheriting from it (not for abstract model inheritance)

old_app

0001_rename_model_table

A migration with AlterModelTable('old_app_mymodel', 'new_app_mymodel')

new_app

0002_create_model

A migration with SeparateDatabaseAndState(state_operations=[CreateModel('MyModel', fields=[...])]). Copy-paste and adjust the CreateModel operation and AddField operations into the state_operations attribute.

others

0003_alter_fields

Point incoming ForeignKeys to the new table with AlterField('somemodel', 'somefield', ForeignKey('new_app.MyModel'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment