python manage.py runserver导致AppRegistryNotReady异常,其中包含扩展ProtectedResourceView的视图

时间:2014-11-07 22:22:46

标签: python django

环境:

  • Ubuntu GNOME 14.04
  • Python 2.7.6(使用virtualenv)
  • Django 1.7.1(在virtualenv内)
  • 自定义用户模型
  • django-oauth-toolkit 0.7.2(通过点子安装)(https://github.com/evonove/django-oauth-toolkit
  • Eclipse Kepler SR2(内部版本号20140224-0627)
  • PyDev 3.8.0.201409251235

我正在使用Django和django-oauth-toolkit为iOS和Android应用程序创建RESTful后端API。我之前已经成功完成了在CentOS 6.5上使用Python 2.6.6和Django 1.4.8的不同移动应用程序,但在当前环境中遇到了麻烦。

我有一个扩展django-oauth-toolkit的ProtectedResourceViewhttp://django-oauth-toolkit.readthedocs.org/en/0.7.2/views/details.html#generic)的类:

from oauth2_provider.views.generic import ProtectedResourceView
from django.http.response import HttpResponse

class TestEndpoint(ProtectedResourceView):

    def get(self, request, *args, **kwargs):
        return HttpResponse("Hello World!")

此视图的文件位于/home/user/workspace/MyProject/MyApp/views/test_endpoint.py。它将导入到/home/user/workspace/MyProject/MyApp/views/__init__.py中的视图包中。

端点映射在我的urls.py中:

from django.conf.urls import patterns, url
from MyApp.views import UnprotectedEndpoint
from MyApp.views.test_endpoint import TestEndpoint

urlpatterns = patterns('api_v1',
    url(r'^account/create/', UnprotectedEndpoint.as_view()),
    url(r'^test/', TestEndpoint.as_view()),
)

进入virtualenv后,我运行python manage.py runserver并收到以下错误:

(MyVirtualEnv)user@ubuntu-laptop:~/workspace/MyProject$ python manage.py runserver
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/user/workspace/MyProject/MyApp/__init__.py", line 1, in <module>
    import urls
  File "/home/user/workspace/MyProject/MyApp/urls.py", line 2, in <module>
    from MyApp.views import UnprotectedEndpoint
  File "/home/user/workspace/MyProject/MyApp/views/__init__.py", line 3, in <module>
    from test_endpoint import TestEndpoint
  File "/home/user/workspace/MyProject/MyApp/views/test_endpoint.py", line 1, in <module>
    from oauth2_provider.views.generic import ProtectedResourceView
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/oauth2_provider/views/__init__.py", line 1, in <module>
    from .base import AuthorizationView, TokenView
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/oauth2_provider/views/base.py", line 13, in <module>
    from ..forms import AllowForm
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/oauth2_provider/forms.py", line 22, in <module>
    class RegistrationForm(forms.ModelForm):
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/oauth2_provider/forms.py", line 26, in RegistrationForm
    class Meta:
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/oauth2_provider/forms.py", line 27, in Meta
    model = get_application_model()
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/oauth2_provider/models.py", line 241, in get_application_model
    app_model = get_model(app_label, model_name)
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/django/apps/registry.py", line 199, in get_model
    self.check_models_ready()
  File "/home/user/virtualenvs/MyVirtualEnv/local/lib/python2.7/site-packages/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.

当我从TestEndpoint__init__.py中删除urls.py时,服务器启动正常。但是当我导入扩展ProtectedResourceView的视图时,会抛出AppRegistryNotReady异常。为什么Django不喜欢这种观点?

1 个答案:

答案 0 :(得分:1)

这是django-oauth-toolkit与Django 1.7一起出现的错误,请参阅此处:https://github.com/evonove/django-oauth-toolkit/issues/151

您可以使用发布到该问题的变通方法,或者只是切换回Django 1.6并等待django-oauth-toolkit的修复。

==========

来自OP的更新

此问题已在django-oauth-toolkit 0.8中修复,并于2015年3月27日发布到PyPI。我已使用原始问题中描述的项目对此进行了测试和确认。