Django 1.9 deprecation warnings app_label

后端 未结 12 755
花落未央
花落未央 2020-12-01 05:09

I\'ve just updated to Django v1.8, and testing my local setup before updating my project and I\'ve had a deprecation warning that I\'ve never seen before, nor does it make a

12条回答
  •  广开言路
    2020-12-01 05:36

    I was getting a similar error, but rather than complaining about models in my apps, it was complaining about models in contrib packages. For example:

    C:\Program Files\Python 2.7\lib\site-packages\django\contrib\sessions\models.py:27: RemovedInDjango19Warning: Model class django.contrib.sessions.models.Session doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9. class Session(models.Model):

    This is caused by a bad ordering in the INSTALLED_APPS property in settings.py. My settings.py initially contained:

    INSTALLED_APPS = (
        'my_app_1',
        'my_app_2',
        'my_app_3',
        'bootstrap_admin',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.messages',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.staticfiles',
        'social.apps.django_app.default',
        'mathfilters',
        'rest_framework',
    )
    

    my_app_* use models from the contrib packages. The error is caused by using models before declaring them (i.e. Django should know about the apps containing those models before using them).

    In order to solve this, the order in which the apps are declared needs to be changed. Specifically, all Django apps should come before the user defined apps. In my case, the correct INSTALLED_APPS would look like:

    INSTALLED_APPS = (
        'bootstrap_admin',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.messages',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.staticfiles',
        'social.apps.django_app.default',
        'mathfilters',
        'rest_framework',
        'my_app_1',
        'my_app_2',
        'my_app_3',
    )
    

    Now I know this might not directly answer your question, but it answers a related one, and since this is the only SO link that shows on Google when pasting the error, I have answered it here.

    However, I believe a similar situation is causing your problem:

    Make sure you declare the "dependency" apps before the apps using them! The errors don't really specify which app is using a model, so you would have to push the app containing the model it mentions to the top, one by one, until the error disappears.

提交回复
热议问题