问题
Since our app has many models, we place them in sub-packages of the models packages, i.e. the Cheddar
model would not be in models.Cheddar
, but instead in models.cheese.Cheddar
.
It seems I cannot access these models in a South datamigration, even though I created an models/__init__.py
as per this answer containing the line from cheese import *
.
In my data migration file, the line for cheddar in orm.Cheddar.objects.all():
still causes the following error:
AttributeError: The model 'Cheddar' from the app 'core' is not available in this migration. (Did you use orm.ModelName, not orm['app.ModelName']?)
Trying to use orm['core.models.cheese.Cheddar']
instead causes this error:
KeyError: "The model 'cheddar' from the app 'core' is not available in this migration."
Does anyone know how to work around this problem?
回答1:
Turns out, the problem was in the fact, that the Cheddar
model was not listed in the DataMigration
instance's models
property:
class Migration(DataMigration):
# ...
models = {
# ...
}
Once I added the correct model definition in there (which was in the previous migration for me), the data migration worked.
来源:https://stackoverflow.com/questions/19400149/django-south-how-can-i-access-models-in-sub-packages-in-migrations