django:AppRegistryNotReady将tastypie添加到INSTALLED APPS后出现异常

时间:2014-08-22 14:46:41

标签: python django tastypie

所以,我现在正在Django 1.7rc2上运行,创建了几个模型,当我运行./manage.py shell时,一切都运行得很好,但是,只要我添加tastypie(我有最后一个)稳定版,0.11.1)到INSTALLED_APPS,我在./manage.py shell上得到了这个例外

Traceback (most recent call last):
  File "manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "/home/miki/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/miki/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/miki/project/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/miki/project/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/miki/project/local/lib/python2.7/site-packages/django/apps/config.py", line 197, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/miki/project/local/lib/python2.7/site-packages/tastypie/models.py", line 31, in <module>
    from tastypie.compat import AUTH_USER_MODEL
  File "/home/miki/project/local/lib/python2.7/site-packages/tastypie/compat.py", line 13, in <module>
    User = get_user_model()
  File "/home/miki/project/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 136, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL)
  File "/home/miki/project/local/lib/python2.7/site-packages/django/apps/registry.py", line 199, in get_model
    self.check_models_ready()
  File "/home/miki/project/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.

我已经搜索了一下,并且只遇到了从独立脚本运行脚本的异常,但事实并非如此。另外,在django文档中它说

Another common culprit is django.contrib.auth.get_user_model(). Use the AUTH_USER_MODEL setting to reference the User model at import time.

但我已在我的设置中设置了该设置

# The model to use to represent a User.
AUTH_USER_MODEL = 'customuser.CustomUser'

我的代码中的错误是某处还是tastypie中的问题?

2 个答案:

答案 0 :(得分:0)

提示的重点不仅仅是您在设置中定义,而是您应该将该设置用作指向用户的任何ForeignKeys的目标,而不是使用get_user_model()。

答案 1 :(得分:-1)

丹尼尔斯回答了我的解决方案,但是因为有一个新版本的tastypie,它解决了这个问题。