我正在跟随tutorial使用python 3.5和Django 1.10来制作后端,但是有一个问题我无法解决。从github下载的作者代码中也会出现此错误。 我在这个问题上花了一天时间,谢谢你的时间。
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/core/management/__init__.py", line 341, in execute
django.setup()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/Charles/djangop1/urban_tastes/services/apps.py", line 2, in <module>
from django.contrib.auth.models import User
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/db/models/base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/apps/registry.py", line 124, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
from django.apps import AppConfig
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from tastypie.models import create_api_key
class ServicesConfig(AppConfig):
name = 'services'
def ready(self):
# This line dispatches signal to Tastypie to create APIKey
signals.post_save.connect(create_api_key, sender=User)
<__init__.py>
default_app_config = 'services.apps.ServiceConfig'
<settings.py>
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tastypie',
'services'
]
答案 0 :(得分:3)
File&#34; /Users/Charles/djangop1/urban_tastes/services/apps.py" ;,第2行,in 来自django.contrib.auth.models导入用户
您的问题是直接在User
中导入app.py
模型。
请参阅Referencing the User model上的文档。
您需要使用AUTH_USER_MODEL
设置或django.contrib.auth.get_user_model()
。
一般来说,您应该在导入时执行的代码中使用AUTH_USER_MODEL设置引用用户模型。 get_user_model()仅在Django导入所有模型后才有效。
在您的情况下,User
处理程序中使用了ready()
,因此get_user_model
可以正常运行。
我希望有所帮助。
答案 1 :(得分:-1)
当我得到它时,当您尝试运行服务器时会发生错误。幸运的是,Django有一个很棒的命令./manage.py validate
。此命令将显示实际发生的情况。