create_user() 缺少 1 个必需的位置参数:'username'
create_user() missing 1 required positional argument: 'username'
我删除了 username
字段,因为我希望用户能够使用他们的电子邮件地址登录,所以我在 models.py
中有这个:
class CustomUser(AbstractUser):
USER_TYPE = ((1, 'HOD'), (2, 'Staff'), (3, 'Student'))
username = None
email = models.EmailField(unique=True)
user_type = models.CharField(default=1, choices=USER_TYPE, max_length=1)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Student(models.Model):
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
gender = models.CharField(max_length=1, choices=GENDER)
address = models.TextField()
profile_pic = models.ImageField(upload_to='media')
session_start_year = models.DateField(null=True)
session_end_year = models.DateField(null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
在我的 views.py
中,我试过:
user = CustomUser.objects.create_user(email=email, password=password, user_type=3, first_name=first_name, last_name=last_name)
但是我 create_user() 缺少 1 个必需的位置参数:'username'
我该如何解决这个问题?
您需要编写 manager for a custom user model [Django-doc] 来重新实现 create_user
方法。
因此您可以实现 CustomUserManager
:
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import UserManager
class <b>CustomUserManager</b>(UserManager):
def _create_user(self, email, password, **extra_fields):
email = self.normalize_email(email)
user = CustomUser(email=email, **extra_fields)
user<b>.password = make_password(password)</b>
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
assert extra_fields['is_staff']
assert extra_fields['is_superuser']
return self._create_user(email, password, **extra_fields)
class CustomUser(AbstractUser):
# …
<b>objects = CustomUserManager()</b>
然后您将此经理注册为 objects
经理。
我删除了 username
字段,因为我希望用户能够使用他们的电子邮件地址登录,所以我在 models.py
中有这个:
class CustomUser(AbstractUser):
USER_TYPE = ((1, 'HOD'), (2, 'Staff'), (3, 'Student'))
username = None
email = models.EmailField(unique=True)
user_type = models.CharField(default=1, choices=USER_TYPE, max_length=1)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Student(models.Model):
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
gender = models.CharField(max_length=1, choices=GENDER)
address = models.TextField()
profile_pic = models.ImageField(upload_to='media')
session_start_year = models.DateField(null=True)
session_end_year = models.DateField(null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
在我的 views.py
中,我试过:
user = CustomUser.objects.create_user(email=email, password=password, user_type=3, first_name=first_name, last_name=last_name)
但是我 create_user() 缺少 1 个必需的位置参数:'username' 我该如何解决这个问题?
您需要编写 manager for a custom user model [Django-doc] 来重新实现 create_user
方法。
因此您可以实现 CustomUserManager
:
from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import UserManager
class <b>CustomUserManager</b>(UserManager):
def _create_user(self, email, password, **extra_fields):
email = self.normalize_email(email)
user = CustomUser(email=email, **extra_fields)
user<b>.password = make_password(password)</b>
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
assert extra_fields['is_staff']
assert extra_fields['is_superuser']
return self._create_user(email, password, **extra_fields)
class CustomUser(AbstractUser):
# …
<b>objects = CustomUserManager()</b>
然后您将此经理注册为 objects
经理。