小编典典

Django 1.7抛出django.core.exceptions.AppRegistryNotReady:模型尚未加载

django

这是Windows系统上的追溯。

Traceback (most recent call last):
  File "D:\AMD\workspace\steelrumors\manage.py", line 9, in <module>
    django.setup()
  File "D:\AMD\Django\django-django-4c85a0d\django\__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "D:\AMD\Django\django-django-4c85a0d\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "D:\AMD\Django\django-django-4c85a0d\django\apps\config.py", line 197, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
  File "C:\Python27\lib\site-packages\registration\models.py", line 15, in <module>
    User = get_user_model()
  File "D:\AMD\Django\django-django-4c85a0d\django\contrib\auth\__init__.py", line 135, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL)
  File "D:\AMD\Django\django-django-4c85a0d\django\apps\registry.py", line 199, in get_model
    self.check_models_ready()
  File "D:\AMD\Django\django-django-4c85a0d\django\apps\registry.py", line 131, in check_models_ready
    raise AppRegistryNotReady("Models aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.

我的manage.py看起来像这样:

import os
import sys
import django

if __name__ == "__main__":

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "steelrumors.settings")
    django.setup()
    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

当我尝试在Django 1.7中使用注册应用程序时出现此错误


阅读 342

收藏
2020-03-25

共3个答案

小编典典

这就是为我们和这些人解决的问题:

我们的项目从Django 1.4开始,然后转到1.5,然后到1.7。我们的wsgi.py看起来像这样:

import os

from django.core.handlers.wsgi import WSGIHandler

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = WSGIHandler()

当我更新到1.7样式的WSGI处理程序时:

import os

from django.core.wsgi import get_wsgi_application

os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()

现在一切正常。

2020-03-25
小编典典

问题出在你的注册应用中。django-registration似乎get_user_module()models.py在模块级别调用的(当应用程序注册过程仍在加载模型时)。这将不再起作用:

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:
    from django.contrib.auth.models import User   

我将更改此模型文件以仅get_user_model()在方法内部调用(而不是在模块级别),并且在FK中使用类似以下内容的方法:

user = ForeignKey(settings.AUTH_USER_MODEL)

顺便说一句,django.setup()你的manage.py文件中不需要调用,而是在中为你调用execute_from_command_line

2020-03-25
小编典典

运行以下命令解决了我的问题:

import django
django.setup()

但是我不确定为什么需要这个。评论将不胜感激。

2020-03-25