我正在尝试使用带有 Djoser 的 Django 创建一个用户。当我尝试创建用户时,我收到错误 ["Unable to create account."]
我正在使用继承自 AbstractUser 的 CustomUser:
class CustomUser(AbstractUser):
MAN = 'man'
WOMAN = 'woman'
GENDER_CHOICES = (
(MAN, 'Man'),
(WOMAN, 'Woman'),
)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
email = models.EmailField(_('Email'), unique=True,db_index=True)
gender = models.CharField("Gender", max_length=30, choices=GENDER_CHOICES, blank=True, null=True)
birth_date = models.DateField("Birth date", blank=True, null=True)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['last_name','first_name',]
objects = CustomUserManager()
用于创建用户的自定义管理器:
class CustomUserManager(BaseUserManager):
def create_user(self, email, first_name, last_name, password, **extra_fields):
if not email:
raise ValueError(_('email is required'))
if not first_name:
raise ValueError(_('first_name is required'))
if not last_name:
raise ValueError(_('last_name is required'))
email = self.normalize_email(email)
user = self.model(email=email,first_name=first_name,last_name=last_name, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, first_name, last_name, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
extra_fields.setdefault('is_active', True)
if extra_fields.get('is_staff') is not True:
raise ValueError(_('superuser must have is_staff=True.'))
if extra_fields.get('is_superuser') is not True:
raise ValueError(_('superuser must have is_superuser=True.'))
return self.create_user(email, first_name, last_name, password, **extra_fields)
在我的 urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('auth/', include('djoser.urls')),
path('auth/', include('djoser.urls.jwt')),
]
创建用户的命令:
curl -X POST http://127.0.0.1:8000/auth/users/ --data 'password=xxxxxx&email=test@email.com&first_name=Charlie&last_name=Lim'
当我尝试使用 te last 命令创建用户时,我收到下一个错误:
["Unable to create account."]
我已按照所有步骤操作并查看了几个教程,但我找不到问题所在。