Django model “doesn't declare an explicit app_label”

后端 未结 28 1772
無奈伤痛
無奈伤痛 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:06

    TL;DR: Adding a blank __init__.py fixed the issue for me.

    I got this error in PyCharm and realised that my settings file was not being imported at all. There was no obvious error telling me this, but when I put some nonsense code into the settings.py, it didn't cause an error.

    I had settings.py inside a local_settings folder. However, I'd fogotten to include a __init__.py in the same folder to allow it to be imported. Once I'd added this, the error went away.

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

    I had exactly the same error when running tests with PyCharm. I've fixed it by explicitly setting DJANGO_SETTINGS_MODULE environment variable. If you're using PyCharm, just hit Edit Configurations button and choose Environment Variables.

    Set the variable to your_project_name.settings and that should fix the thing.

    It seems like this error occurs, because PyCharm runs tests with its own manage.py.

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

    I had this error today trying to run Django tests because I was using the shorthand from .models import * syntax in one of my files. The issue was that I had a file structure like so:

        apps/
          myapp/
            models/
              __init__.py
              foo.py
              bar.py
    

    and in models/__init__.py I was importing my models using the shorthand syntax:

        from .foo import *
        from .bar import *
    

    In my application I was importing models like so:

        from myapp.models import Foo, Bar
    

    This caused the Django model doesn't declare an explicit app_label when running ./manage.py test.

    To fix the problem, I had to explicitly import from the full path in models/__init__.py:

        from myapp.models.foo import *
        from myapp.models.bar import *
    

    That took care of the error.

    H/t https://medium.com/@michal.bock/fix-weird-exceptions-when-running-django-tests-f58def71b59a

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

    In my case I got this error when porting code from Django 1.11.11 to Django 2.2. I was defining a custom FileSystemStorage derived class. In Django 1.11.11 I was having the following line in models.py:

    from django.core.files.storage import Storage, DefaultStorage
    

    and later in the file I had the class definition:

    class MyFileStorage(FileSystemStorage):
    

    However, in Django 2.2 I need to explicitly reference FileSystemStorage class when importing:

    from django.core.files.storage import Storage, DefaultStorage, FileSystemStorage
    

    and voilà!, the error dissapears.

    Note, that everyone is reporting the last part of the error message spitted by Django server. However, if you scroll up you will find the reason in the middle of that error mambo-jambo.

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

    In my case I was getting this error when trying to run python manage.py runserver when not connected to my project's virtual environment.

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

    I had a similar issue, but I was able to solve mine by specifying explicitly the app_label using Meta Class in my models class

    class Meta:
        app_label  = 'name_of_my_app'
    
    0 讨论(0)
提交回复
热议问题