Django South: How can I access models in sub-packages in migrations

浪尽此生 提交于 2019-12-24 03:19:37

问题


Since our app has many models, we place them in sub-packages of the models packages, i.e. the Cheddarmodel 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 modelsproperty:

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!