Getting AppRegistryNotReady("Models aren't loaded yet.") With a Custom User Model

1,875 views
Skip to first unread message

CLIFFORD ILKAY

unread,
Oct 4, 2016, 4:44:35 AM10/4/16
to Wagtail support
Hello,

I have a custom User model as below with Wagtail 1.6.3 and Django
1.10.2. When I attempt to ./manage.py createsuperuser, the
AppRegistryNotReady("Models aren't loaded yet.") exception is thrown.
Here is my code.

########### settings.py

INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'compressor',
'taggit',
'modelcluster',
'rest_framework',
'django.contrib.admin',
'wagtail.wagtailcore',
'wagtail.wagtailadmin',
'wagtail.wagtaildocs',
'wagtail.wagtailsnippets',
'wagtail.wagtailusers',
'wagtail.wagtailimages',
'wagtail.wagtailembeds',
'wagtail.wagtailsearch',
'wagtail.wagtailredirects',
'wagtail.wagtailforms',
'wagtail.wagtailsites',
'wagtail.contrib.wagtailapi',
'wagtail.contrib.modeladmin',
'cdssite',
)

AUTH_USER_MODEL = 'cdssite.User'
WAGTAIL_USER_EDIT_FORM = 'users.forms.CustomUserEditForm'
WAGTAIL_USER_CREATION_FORM = 'users.forms.CustomUserCreationForm'
WAGTAIL_USER_CUSTOM_FIELDS = ['customer_name']

########### models.py

from django.db import models
from django import forms
from django.contrib.auth.models import AbstractUser
from wagtail.wagtailusers.forms import UserEditForm, UserCreationForm

class CustomerGroup(models.Model):
name = models.CharField('Group Name', max_length=64, unique=True)

def __str__(self):
return '%s' % (self.name,)

class Customer(models.Model):
name = models.CharField('Customer Name', max_length=64, unique=True)
customer_group = models.ForeignKey(CustomerGroup, on_delete=models.CASCADE)
list_display = ('get_customer_group',)

def __str__(self):
return '%s' % (self.name,)

class User(AbstractUser):
customer_name = models.ForeignKey(Customer)

class CustomUserEditForm(UserEditForm):
customer_name = forms.ModelChoiceField(queryset=Customer.objects, required=True)

class CustomUserCreationForm(UserCreationForm):
customer_name = forms.ModelChoiceField(queryset=Customer.objects, required=True)

The create.html and edit.html forms are in
cdssite/templates/wagtailusers/users. What I am trying to accomplish is
having a custom User model with one extra field, customer_name, which is
related to the Customer model. One Customer can have many Users. One
CustomerGroup can have multiple Customers.

Why am I getting the AppRegistryNotReady("Models aren't loaded yet.")
exception?

--
Regards,

Clifford

CLIFFORD ILKAY

unread,
Oct 4, 2016, 4:47:37 AM10/4/16
to wag...@googlegroups.com
I should add that there are no other models in my app right now in order
to have the custom User model be created before any other models are
created.

Matthew Westcott

unread,
Oct 4, 2016, 8:10:28 AM10/4/16
to wag...@googlegroups.com
Hi Clifford,

The problem here is that the line:
from wagtail.wagtailusers.forms import UserEditForm, UserCreationForm
is causing a circular import - the wagtail.wagtailusers.forms module depends on various other models within Wagtail, which in turn depend on your user model.

You shouldn't put your CustomUserEditForm / CustomUserCreationForm definitions in models.py - these (along with the import line above) should go in a separate forms.py file in your app. You'll then need to update the WAGTAIL_USER_EDIT_FORM / WAGTAIL_USER_CREATION_FORM settings to point to that location:

WAGTAIL_USER_EDIT_FORM = 'cdssite.forms.CustomUserEditForm'
WAGTAIL_USER_CREATION_FORM = 'cdssite.forms.CustomUserCreationForm'


Cheers,
- Matt
Reply all
Reply to author
Forward
0 new messages