Django model “doesn't declare an explicit app_label”

后端 未结 28 1763
無奈伤痛
無奈伤痛 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!

    0 讨论(0)
  • 2020-11-27 16:02

    Are you missing putting in your application name into the settings file? The myAppNameConfig is the default class generated at apps.py by the .manage.py createapp myAppName command. Where myAppName is the name of your app.

    settings.py

    INSTALLED_APPS = [
    'myAppName.apps.myAppNameConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ]
    

    This way, the settings file finds out what you want to call your application. You can change how it looks later in the apps.py file by adding the following code in

    myAppName/apps.py

    class myAppNameConfig(AppConfig):
        name = 'myAppName'
        verbose_name = 'A Much Better Name'
    
    0 讨论(0)
  • 2020-11-27 16:03

    If all else fails, and if you are seeing this error while trying to import in a PyCharm "Python console" (or "Django console"):

    Try restarting the console.

    This is pretty embarassing, but it took me a while before I realized I had forgotten to do that.

    Here's what happened:

    Added a fresh app, then added a minimal model, then tried to import the model in the Python/Django console (PyCharm pro 2019.2). This raised the doesn't declare an explicit app_label error, because I had not added the new app to INSTALLED_APPS. So, I added the app to INSTALLED_APPS, tried the import again, but still got the same error.

    Came here, read all the other answers, but nothing seemed to fit.

    Finally it hit me that I had not yet restarted the Python console after adding the new app to INSTALLED_APPS.

    Note: failing to restart the PyCharm Python console, after adding a new object to a module, is also a great way to get a very confusing ImportError: Cannot import name ...

    0 讨论(0)
  • 2020-11-27 16:05

    I had the same problem just now. I've fixed mine by adding a namespace on the app name. Hope someone find this helpful.

    apps.py

    from django.apps import AppConfig    
    
    class SalesClientConfig(AppConfig):
            name = 'portal.sales_client'
            verbose_name = 'Sales Client'
    
    0 讨论(0)
  • 2020-11-27 16:05

    In my case, this was happening because I used a relative module path in project-level urls.py, INSTALLED_APPS and apps.py instead of being rooted in the project root. i.e. absolute module paths throughout, rather than relative modules paths + hacks.

    No matter how much I messed with the paths in INSTALLED_APPS and apps.py in my app, I couldn't get both runserver and pytest to work til all three of those were rooted in the project root.

    Folder structure:

    |-- manage.py
    |-- config
        |-- settings.py
        |-- urls.py
    |-- biz_portal
        |-- apps
            |-- portal
                |-- models.py
                |-- urls.py
                |-- views.py
                |-- apps.py
    

    With the following, I could run manage.py runserver and gunicorn with wsgi and use portal app views without trouble, but pytest would error with ModuleNotFoundError: No module named 'apps' despite DJANGO_SETTINGS_MODULE being configured correctly.

    config/settings.py:

    INSTALLED_APPS = [
        ...
        "apps.portal.apps.PortalConfig",
    ]
    

    biz_portal/apps/portal/apps.py:

    class PortalConfig(AppConfig):
        name = 'apps.portal'
    

    config/urls.py:

    urlpatterns = [
        path('', include('apps.portal.urls')),
        ...
    ]
    

    Changing the app reference in config/settings.py to biz_portal.apps.portal.apps.PortalConfig and PortalConfig.name to biz_portal.apps.portal allowed pytest to run (I don't have tests for portal views yet) but runserver would error with

    RuntimeError: Model class apps.portal.models.Business doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS

    Finally I grepped for apps.portal to see what's still using a relative path, and found that config/urls.py should also use biz_portal.apps.portal.urls.

    0 讨论(0)
  • 2020-11-27 16:05

    O...M...G I was getting this error too and I spent almost 2 days on it and now I finally managed to solve it. Honestly...the error had nothing to do with what the problem was. In my case it was a simple matter of syntax. I was trying to run a python module standalone that used some django models in a django context, but the module itself wasn't a django model. But I was declaring the class wrong

    instead of having

    class Scrapper:
        name = ""
        main_link= ""
        ...
    

    I was doing

    class Scrapper(Website):
        name = ""
        main_link= ""
        ...
    

    which is obviously wrong. The message is so misleading that I couldn't help myself but think it was some issue with configuration or just using django in a wrong way since I'm very new to it.

    I'll share this here for someone newbie as me going through the same silliness can hopefully solve their issue.

    0 讨论(0)
提交回复
热议问题