使用Django + Djoser创建帐户[“无法创建帐户。”]

时间:2020-06-24 22:20:51

标签: python django djoser

我正在尝试将Django与Djoser结合使用。尝试创建用户时,出现错误[“无法创建帐户。”]

我正在使用从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."]                                                                                                                                                                           

我已经按照所有步骤操作,并阅读了一些教程,但找不到问题。

1 个答案:

答案 0 :(得分:0)

这是一个愚蠢的小错误,很容易解决。

AbstractUser 具有一个 username 字段,该字段是唯一键。在我的程序中,用户名不是必需的,当我尝试创建多个用户时,将使用参数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."),
        },
    )
...