Python Django 1.7 抛出 django.core.exceptions.AppRegistryNotReady:模型尚未加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25537905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
提问by doubleo
This is the traceback on my windows system.
这是我的 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.
And my manage.py looks like this:
我的 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)
I get this error when i am trying to use registrationapp in Django 1.7
当我尝试在 Django 1.7 中使用注册应用程序时出现此错误
采纳答案by Nick Spacek
This is what solved it for us and these folks:
这就是为我们和这些人解决的问题:
Our project started with Django 1.4, we went to 1.5 and then to 1.7. Our wsgi.py looked like this:
我们的项目从 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()
When I updated to the 1.7 style WSGI handler:
当我更新到 1.7 样式的 WSGI 处理程序时:
import os
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()
Everything works now.
现在一切正常。
回答by gonz
The issue is in your registration app. It seems django-registration calls get_user_module()in models.pyat a module level (when models are still being loaded by the application registration process). This will no longer work:
问题出在您的注册应用程序中。似乎 django-registrationget_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
I'd change this models file to only call get_user_model()inside methods (and not at module level) and in FKs use something like:
我get_user_model()会将这个模型文件更改为仅调用内部方法(而不是在模块级别),并且在 FK 中使用如下内容:
user = ForeignKey(settings.AUTH_USER_MODEL)
BTW, the call to django.setup()shouldn't be required in your manage.pyfile, it's called for you in execute_from_command_line. (source)
顺便说一句,django.setup()您的manage.py文件中不需要调用 ,而是在execute_from_command_line. (来源)
回答by Kristian Glass
Your manage.pyis "wrong"; I don't know where you got it from, but that's not a 1.7 manage.py- were you using some funky pre-release build or something?
你manage.py“错了”;我不知道你从哪里得到它,但这不是 1.7 manage.py- 你使用的是一些时髦的预发布版本吗?
Reset your manage.pyto the conventional, as below, and things Should Just Work:
将您的重置manage.py为常规,如下所示,事情应该可以正常工作:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
回答by Nimo
Running these commands solved my problem (credit to this answer):
运行这些命令解决了我的问题(归功于此答案):
import django
django.setup()
However I'm not sure why I need this. Comments would be appreciated.
但是我不确定为什么我需要这个。评论将不胜感激。
回答by Travis
Do you have a Python virtual environment that you need to enter before you run manage.py?
在运行 manage.py 之前,您是否有需要输入的 Python 虚拟环境?
I ran into this error myself, and that was the problem.
我自己遇到了这个错误,这就是问题所在。
回答by hails
Just encountered the same issue. The problem is because of django-registrationincompatible with django 1.7 user model.
刚遇到同样的问题。问题是因为django-registration与 django 1.7 用户模型不兼容。
A simple fix is to change these lines of code, at your installed django-registrationmodule::
一个简单的解决方法是在您安装的django-registration模块中更改这些代码行:
try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError:
from django.contrib.auth.models import User
to::
到::
from django.conf import settings
try:
from django.contrib.auth import get_user_model
User = settings.AUTH_USER_MODEL
except ImportError:
from django.contrib.auth.models import User
Mine is at .venv/local/lib/python2.7/site-packages/registration/models.py(virtualenv)
我的是.venv/local/lib/python2.7/site-packages/registration/models.py(virtualenv)
回答by pabo
I ran into this issue when I use djangocms and added a plugin (in my case: djangocms-cascade). Of course I had to add the plugin to the INSTALLED_APPS. But the order is here important.
我在使用 djangocms 并添加了一个插件(在我的例子中:djangocms-cascade)时遇到了这个问题。当然,我必须将插件添加到 INSTALLED_APPS。但顺序在这里很重要。
To place 'cmsplugin_cascade' before'cms' solved the issue.
在“cms”解决问题之前放置“cmsplugin_cascade” 。
回答by user2350206
install django-registration-redux==1.1 instead django-registration, if you using django 1.7
如果您使用 django 1.7,请安装 django-registration-redux==1.1 而不是 django-registration
回答by Mark
Another option is that you have a duplicate entry in INSTALLED_APPS. That threw this error for two different apps I tested. Apparently it's not something Django checks for, but then who's silly enough to put the same app in the list twice. Me, that's who.
另一种选择是您在 INSTALLED_APPS 中有重复的条目。这为我测试的两个不同的应用程序抛出了这个错误。显然这不是 Django 检查的东西,但是谁傻到把同一个应用程序放在列表中两次。我,就是那个人。
回答by Lorenzo Lerate
This works for me for Django 1.9 . The Python script to execute was in the root of the Django project.
这对我适用于 Django 1.9 。要执行的 Python 脚本位于 Django 项目的根目录中。
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_NAME.settings")
django.setup()
from APP_NAME.models import *
Set PROJECT_NAME and APP_NAME to yours
将 PROJECT_NAME 和 APP_NAME 设置为您的

