1

我正在尝试使用带有 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."]                                                                                                                                                                           

我已按照所有步骤操作并查看了几个教程,但我找不到问题所在。

4

1 回答 1

1

这是一个很容易修复的愚蠢的小错误。

AbstractUser有一个用户名字段,它是一个唯一键。在我的程序中,用户名不是强制性的,当我尝试创建多个用户时,这些用户是使用参数 blank=False null=False 创建的,从而引发错误。

解决办法:在CustomUser类中重新定义这个字段,用blank=True null=True

class CustomUser(AbstractUser):
...
    username = models.CharField(
        _('username'),
        max_length=150,
        unique=True,
        blank=True, 
        null=True,
        help_text=_('Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.'),
        validators=[username_validator],
        error_messages={
            'unique': _("A user with that username already exists."),
        },
    )
...
于 2020-07-02T01:00:28.693 回答