问题:Django 1.7抛出django.core.exceptions.AppRegistryNotReady:模型尚未加载
这是我的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中使用注册应用程序时收到此错误
This is the traceback on my windows system.
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:
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 registration app in Django 1.7
回答 0
这就是为我们和这些人解决的问题:
我们的项目从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()
现在一切正常。
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:
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:
import os
from django.core.wsgi import get_wsgi_application
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
application = get_wsgi_application()
Everything works now.
回答 1
运行这些命令解决了我的问题(此答案的积分):
import django
django.setup()
但是我不确定为什么需要这个。评论将不胜感激。
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.
回答 2
问题出在您的注册应用中。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
。(来源)
The issue is in your registration app. It seems django-registration calls get_user_module()
in models.py
at a module level (when models are still being loaded by the application registration process). This will no longer work:
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:
user = ForeignKey(settings.AUTH_USER_MODEL)
BTW, the call to django.setup()
shouldn’t be required in your manage.py
file, it’s called for you in execute_from_command_line
. (source)
回答 3
刚遇到同样的问题。问题是因为django-registration
与django 1.7用户模型不兼容。
一个简单的解决方法是在已安装的django-registration
模块上更改以下代码行:
try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError:
from django.contrib.auth.models import User
至::
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
我的在.venv/local/lib/python2.7/site-packages/registration/models.py
(virtualenv)
Just encountered the same issue. The problem is because of django-registration
incompatible with django 1.7 user model.
A simple fix is to change these lines of code, at your installed django-registration
module::
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)
回答 4
这对我适用于Django 1.9。要执行的Python脚本位于Django项目的根目录中。
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "PROJECT_NAME.settings")
django.setup()
from APP_NAME.models import *
将PROJECT_NAME和APP_NAME设置为您的
This works for me for Django 1.9 . The Python script to execute was in the root of the Django project.
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
回答 5
另一个选择是您在INSTALLED_APPS中有一个重复的条目。这引发了我测试的两个不同应用程序的错误。显然,这不是Django所要检查的事情,但是谁又傻又足以将同一应用程序两次放入列表中。我,那是谁。
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.
回答 6
在运行manage.py之前,您是否需要输入Python虚拟环境?
我自己遇到了这个错误,那就是问题所在。
Do you have a Python virtual environment that you need to enter before you run manage.py?
I ran into this error myself, and that was the problem.
回答 7
使用djangocms并添加插件(在我的情况下:djangocms-cascade)时遇到了这个问题。当然,我必须将插件添加到INSTALLED_APPS。但是顺序在这里很重要。
将“ cmsplugin_cascade”放置在 “ cms” 之前即可解决此问题。
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.
To place ‘cmsplugin_cascade’ before ‘cms’ solved the issue.
回答 8
如果使用django 1.7,请安装django-registration-redux == 1.1代替django-registration
install django-registration-redux==1.1 instead django-registration, if you using django 1.7
回答 9
./manage.py migrate
这解决了我的问题
./manage.py migrate
This solved my issue
回答 10
您manage.py
的“错误”;我不知道您从哪里得到它,但是那不是1.7- manage.py
您使用的是一些时髦的预发布版本还是什么?
将您的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)
Your manage.py
is “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?
Reset your manage.py
to the conventional, as below, and things Should Just Work:
#!/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)