【发布时间】:2020-04-06 04:08:37
【问题描述】:
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
# Create your models here.
class MyAccountManager(BaseUserManager):
def create_user(self, email, username, password=None):
if not email:
raise ValueError('user must have smartcard')
if not username:
raise ValueError('must have username')
user = self.create_user(
email=self.normalize_email(email),
username=username,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, username, password):
user = self.create_user(
email=self.normalize_email(email),
password=password,
username=username,
)
user.is_admin =True
user.is_staff =True
user.is_superuser=True
user.save(using=self._db)
return user
class Account(AbstractBaseUser):
username = models.CharField(max_length=100, unique=True)
email = models.EmailField(verbose_name="email", max_length=30, unique=True)
department = models.CharField(max_length=30, choices=DEPARTMENT)
USERNAME_FIELD = 'email'
REQUIRED_FIELD = ['username',]
objects=MyAccountManager()
def __str__(self):
return self.email
def has_perm(self, perm, obj=None):
return self.is_admin
def has_module_perms(self, app_label):
return True
我就是不知道哪里出了问题。
创建超级用户时提示不要求我输入用户名
我也修改了 settings.py 文件。 用各种数据库尝试了几次。 我还在 REQUIRED_FIELDS 部分添加了用户名。
【问题讨论】:
-
在您的问题中包含堆栈跟踪。
标签: django django-models django-authentication