Django migrations reference a remote module
I have one Model
with a name FooModel
defined in my_app/models/foo.py
.
After uninstalling foo.py
, running Django migration (1.7) throws an error because the old migration files import foo.py
( import myapp.models.foo.FooModel
).
How can I resolve this?
This happens when the model has ImageField
a parameter upload_to
.
source to share
There are two cases:
-
You moved
FooModel
to a different location and then edit all the migration files to reflect this move. -
You uninstalled
FooModel
, in this case follow these steps:- Place
FooModel
back where it was. - Make sure your code doesn't reference
FooModel
. - Run
./manage.py makemigrations my_app
- Run
./manage.py squashmigrations my_app <migration created with the previous comand>
- see the doc for more information on suppressing migration. - Repeat the previous two steps for any application that references
FooModel
its migrations. - Remove
FooModel
and obsolete migration files after you've ensured everything is working fine.
- Place
This should work because since it FooModel
doesn't reference any other model, it must be removed from the migration files when they are uploaded.
However, be warned that flashing migrations is not a straightforward operation and can have consequences, it might be a better idea to just keep the model in your codebase without using it.
Note: In this situation, the object in question is a Django model, but this applies to any class, function or module that is referenced by the migration files.
source to share