Django:未加载用户用户模型

时间:2017-03-01 14:53:54

标签: python django python-2.7 django-models

from django.contrib.auth import get_user_model
from django import forms

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = get_user_model()
        fields = ['username', 'email', 'password']

我得到:" django.core.exceptions.AppRegistryNotReady:模型尚未加载。"

Traceback (most recent call last):
  File "./manage.py", line 8, in <module>
    django.setup()
  File "/usr/local/lib/python2.7/dist-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/daniel/testingChamber/djangoTut/home/__init__.py", line 1, in <module>
    import views
  File "/home/daniel/testingChamber/djangoTut/home/views.py", line 9, in <module>
    from .forms import UserForm
  File "/home/daniel/testingChamber/djangoTut/home/forms.py", line 4, in <module>
    class UserForm(forms.ModelForm):
  File "/home/daniel/testingChamber/djangoTut/home/forms.py", line 7, in UserForm
    class Meta:
  File "/home/daniel/testingChamber/djangoTut/home/forms.py", line 8, in Meta
    model = get_user_model()
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/__init__.py", line 163, in get_user_model
    return django_apps.get_model(settings.AUTH_USER_MODEL)
  File "/usr/local/lib/python2.7/dist-packages/django/apps/registry.py", line 192, in get_model
    self.check_models_ready()
  File "/usr/local/lib/python2.7/dist-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的新手。

我使用的版本是1.10.6和Python 2.7.12。

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

如果@Mayank在评论中提出的解决方案不起作用。这是一种解决方法 - 以某种方式在导入设置时导入表单文件。确保不要导入最终导入表单文件的设置中的任何文件。不要在__init__文件中导入此文件。