Reputation: 8101
I am creating a custom django user and I get the following error when trying to use it. The error I get is the following
AppRegistryNotReady: Models aren't loaded yet.
My model
class MyCustomUserManager(BaseUserManager):
def create_user(self, username, email, password=None, **kwargs):
if not username:
raise ValueError("Username must be defined for users")
if not kwargs.get('email'):
raise ValueError("Users must have a valid email")
user = self.model(username=username, email=self.normalize_email(email), **kwargs)
user.set_password(password)
user.save()
return user
def create_superuser(self, username, email, password, **kwargs):
user = self.create_user(username, email, password, **kwargs)
user.is_admin = True
user.save()
class MyCustomUser(AbstractBaseUser):
username = models.CharField(max_length=40, unique=True)
email = models.EmailField(max_length=255, unique=True, verbose_name=_('Email Address'))
date_of_birth = models.DateField(auto_now=False, blank=False, null=False, verbose_name=_("Ημ. Γεννήσεως"))
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
first_name = models.CharField(max_length=40, blank=False, null=False, verbose_name=_("Όνομα"))
last_name = models.CharField(max_length=40, blank=False, null=False, verbose_name=_("Επίθετο"))
friends = models.ManyToManyField('self', related_name="friends")
objects = MyCustomUserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email',]
def get_full_name(self):
return "%s %s" % (self.first_name, self.last_name)
def __unicode__(self):
return self.get_full_name()
class Meta:
verbose_name = _('Χρήστης')
verbose_name_plural = _('Χρήστες')
I have also setup settings file to include my new custom user model.
(inside settings.py)
AUTH_USER_MODEL = 'myapp.MyCustomUserModel'
When logging in the shell trying to use my new user type like this
from django.contrib.auth import get_user_model
user = get_user_model().objects.all()
i get the error above. Is there something wrong with Django 1.7
P.S: I used makemigrations and migrate to make models and I am behind a virtualenv.
Upvotes: 4
Views: 445
Reputation: 20563
This post has been here for some time, I have come across this same problem today (using Django shell) and would like to share a quick workaround by writing a custom get_user_model()
using import_module
.
under one of your models.py
:
from django.conf import settings
from importlib import import_module
USER_MODEL = settings.AUTH_USER_MODEL.rsplit('.', 1)
def get_user_model():
models = getattr(import_module(USER_MODEL[0]), 'models')
return getattr(models, USER_MODEL[1])
This works in my local machine (Mac) under Django shell, and I hope this will also work under PyCharm shell.
Upvotes: 0
Reputation: 10172
Starting in django 1.7 you can't use get_user_model in your models.py files or you'll run into this problem. The details/reason can be seen in referencing the user model
To fix this problem, in your models.py files instead of referencing the custom user model with get_user_model, you need to import the name from settings.
from django.conf import settings
class MyModel(models.Model):
...
user = models.ForeignKey(settings.AUTH_USER_MODEL)
When you fix the problem, it will fix your app as well as your shell.
Upvotes: 2