我正在尝试使用用户名和必需的电子邮件字段创建自定义用户模型,并且我正在通过 https://docs.djangoproject.com/en/dev/topics/auth/customizing/中的示例进行此操作#a-full-example 这是我的models.py:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from utils.upload import get_upload_path
class ProfileManager(BaseUserManager):
def create_user(self, username, email, password=None):
if not username:
raise ValueError("User must have a username.")
if not email:
raise ValueError("User must have an email address.")
user = self.model(
username = self.username,
email = self.normalize_email(email),
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, username, email, password):
user = self.create_user(username, email, password=password)
user.is_admin = True
user.save(using=self._db)
return user
class Profile(AbstractBaseUser):
username = models.CharField(db_index=True, unique=True, max_length=30, verbose_name=u"username")
email = models.EmailField(db_index=True, unique=True, max_length=255, verbose_name=u"e-mail address")
# Profile information
first_name = models.CharField(blank=True, null=True, max_length=30, verbose_name=u"first name")
last_name = models.CharField(blank=True, null=True, max_length=30, verbose_name=u"last name")
city = models.CharField(blank=True, null=True, max_length=30, verbose_name=u"city")
country = models.CharField(blank=True, null=True, max_length=30, verbose_name=u"country")
# Forum information
avatar = models.ImageField(blank=True, null=True, upload_to=get_upload_path("avatars"), max_length=255)
signature = models.TextField(blank=True, null=True, verbose_name=u"signature")
# Contact info
icq = models.CharField(blank=True, null=True, max_length=16, verbose_name=u"ICQ number")
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = ProfileManager()
USERNAME_FIELD = "username"
REQUIRED_FIELDS = ["email"]
def get_full_name(self):
return self.username
def get_short_name(self):
return self.username
def __unicode__(self):
return self.username
def has_perm(self, perm, obj=None):
return True
def has_module_perms(self, app_label):
return True
@property
def is_staff(self):
return self.is_admin
当我python manage.py syncdb
第一次运行命令,创建一个新数据库并创建一个超级用户时,我得到了这个错误:
'ProfileManager' object has no attribute 'username'