运行createsuperuser命令后出现以下错误 我不需要归档用户名,但是即使添加了用户名也仍然遇到相同的错误 当我从那里创建超级用户时,错误仅在命令提示符下发生。 这是我的models.py 我不知道我的错误在哪里? 谢谢您提供任何帮助
enter code herefrom django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
from django.core.validators import RegexValidator
from django.contrib.auth.models import UserManager
class CustomUser(AbstractBaseUser):
first_name = models.CharField(max_length=255,null=False, blank=False)
first_last = models.CharField(max_length=255,null=False, blank=False)
email = models.EmailField(max_length=255, unique=True,)
phone_regex = RegexValidator(regex=r'^09(1[0-9]|3[1-9]|2[1-9])-? [0-9]{7}$')
phone_number = models.CharField(validators=[phone_regex], max_length=11,null=False, blank=False, unique=True)
img = models.ImageField(upload_to='documents/',default=None)
active = models.BooleanField(default=True)
staff = models.BooleanField(default=False) # a admin user; non super-user
admin = models.BooleanField(default=False)
# notice the absence of a "Password field", that's built in.
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []# Email & Password are required by default.
objects = UserManager()
def get_full_name(self):
# The user is identified by their email address
return self.email
def get_short_name(self):
# The user is identified by their email address
return self.email
def __str__(self): # __unicode__ on Python 2
return self.email
@staticmethod
def has_perm(perm, obj=None):
# "Does the user have a specific permission?"
# Simplest possible answer: Yes, always
return True
@staticmethod
def has_module_perms(app_label):
# "Does the user have permissions to view the app `app_label`?"
# Simplest possible answer: Yes, always
return True
@property
def is_staff(self):
# "Is the user a member of staff?"
return self.staff
@property
def is_admin(self):
# "Is the user a admin member?"
return self.admin
@property
def is_active(self):
# "Is the user active?"
return self.active
class UserManager(BaseUserManager):
def create_user(self, email, first_name,last_name, phone_number=None, img=None, password=None, is_admin=False, is_staff=False, is_active=False):
if not email:
raise ValueError("User must have an email")
if not password:
raise ValueError("User must have a password")
if not phone_number:
raise ValueError("User must have a password")
CustomUser.set_password(password) # change password to hash
CustomUser.admin = is_admin
CustomUser.img = img
CustomUser.staff = is_staff
CustomUser.active = is_active
CustomUseruser = self.model(
email=self.normalize_email(email)
)
CustomUser.first_name = first_name
CustomUser.last_name = last_name
CustomUser.phone_number = phone_number
CustomUser.save(using=self._db)
return CustomUser
def create_staffuser(self, email, first_name,last_name, phone_number=None, img=None, password=None, is_admin=False, is_staff=False, is_active=True):
CustomUser = self.create_user(
username,
email,
first_name,
last_name,
phone_number,
img,
password=password,
is_staff=True,
is_active=True,
)
return CustomUser
def create_superuser(self,email, first_name,last_name, phone_number=None, img=None, password=None, is_admin=True, is_staff=True, is_active=True):
CustomUser = self.create_user(
username,email, first_name,last_name,phone_number,img,
password=password,
is_staff=True,
is_admin=True,
)
return CustomUser