1
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']

I'm getting: "django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet."

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.

I don't know what's happening. I'm new to Django.

The version I'm using is 1.10.6 and Python 2.7.12.

What's the correct way to do this?

Daniel Gonzalez
  • 63
  • 1
  • 1
  • 4

1 Answers1

3

If solution suggested by @Mayank in comments doesn't work. Here's a workaround - somehow your forms file is being imported at the time of the settings import. Make sure not to import any file in settings that is ultimately importing your form file. Don't import this file in __init__ file either.

hspandher
  • 15,934
  • 2
  • 32
  • 45