我正在创建一个自定义的Django用户,但在尝试使用它时出现以下错误。我收到的错误信息如下:
AppRegistryNotReady: Models aren't loaded yet.
我的模型
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 = _('Χρήστες')
我已经设置了settings文件,以包含我的新自定义用户模型。
(在settings.py文件中)
AUTH_USER_MODEL = 'myapp.MyCustomUserModel'
当尝试使用我的新用户类型登录shell时,请像这样操作:
from django.contrib.auth import get_user_model
user = get_user_model().objects.all()
我遇到了上述错误。这是Django 1.7的问题吗?
附言:我使用makemigrations和migrate制作了模型,并且在虚拟环境中。
import django
和django.setup()
吗? - thefourtheye