Django model “doesn't declare an explicit app_label”

后端 未结 28 1991
無奈伤痛
無奈伤痛 2020-11-27 15:38

I\'m at wit\'s end. After a dozen hours of troubleshooting, probably more, I thought I was finally in business, but then I got:

Model class django.contrib.co         


        
28条回答
  •  天命终不由人
    2020-11-27 16:01

    I just ran into this issue and figured out what was going wrong. Since no previous answer described the issue as it happened to me, I though I would post it for others:

    • the issue came from using python migrate.py startapp myApp from my project root folder, then move myApp to a child folder with mv myApp myFolderWithApps/.
    • I wrote myApp.models and ran python migrate.py makemigrations. All went well.
    • then I did the same with another app that was importing models from myApp. Kaboom! I ran into this error, while performing makemigrations. That was because I had to use myFolderWithApps.myApp to reference my app, but I had forgotten to update MyApp/apps.py. So I corrected myApp/apps.py, settings/INSTALLED_APPS and my import path in my second app.
    • but then the error kept happening: the reason was that I had migrations trying to import the models from myApp with the wrong path. I tried to correct the migration file, but I went at the point where it was easier to reset the DB and delete the migrations to start from scratch.

    So to make a long story short: - the issue was initially coming from the wrong app name in apps.py of myApp, in settings and in the import path of my second app. - but it was not enough to correct the paths in these three places, as migrations had been created with imports referencing the wrong app name. Therefore, the same error kept happening while migrating (except this time from migrations).

    So... check your migrations, and good luck!

提交回复
热议问题