Search code examples
pythondjangodjoser

Account creation with Django + Djoser ["Unable to create account."]


I'm trying to create a user using Django with Djoser. When I try to create the user I get the error ["Unable to create account."]

I'm using a CustomUser that inherits from AbstractUser:

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()

A custom manager for user creation:

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)

In my urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('auth/', include('djoser.urls')),
    path('auth/', include('djoser.urls.jwt')),
]

Command to create an user:

curl -X POST http://127.0.0.1:8000/auth/users/ --data 'password=xxxxxx&email=test@email.com&first_name=Charlie&last_name=Lim'

When I'm trying to create the user with te last command, I receive the next error:

["Unable to create account."]                                                                                                                                                                           

I have followed all the steps and reviewed several tutorials but I can't find the problem.


Solution

  • It was a stupid little error that was easy to fix.

    AbstractUser has a username field which is a unique key. In my program the username is not mandatory and when I try to create several users, these are created with the parameters blank=False null=False, throwing the error.

    The solution: redefine this field in the CustomUser class with 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."),
            },
        )
    ...