Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: create_superuser() got an unexpected keyword argument 'email'

I need to create custom user model because I need to have one additional field called role. If the role is provided, it should be assigned to the user model otherwise provide agent as role in user model. I inherited the AbstractUser. This way when creating the user, first the password is saved as plain text and another is user cannot login it says unable to login. So i tried the below way as in docs but the problem is i don't need the email part so i excluded it and used just username and password but getting error like TypeError: create_superuser() got an unexpected keyword argument 'email'

class UserManager(BaseUserManager):
        def create_user(self, username, password=None):
            """
            Creates and saves a User with the given username, date of
            birth and password.
            """
            if not username:
                raise ValueError('Users must have an username')
            user = self.model(username=username)
            user.set_password(password)
            user.save(using=self._db)
            return user

    def create_superuser(self, username, password):
        """
        Creates and saves a superuser with the given username and password.
        """
        user = self.create_user(
            username=username,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class Role(models.Model):

    ROLE_CHOICES = (
        ('agent', 'Agent'),
        ('agency', 'Agency'),
        ('manufacturer', 'Manufacturer'),
    )
    role = models.CharField(max_length=15, choices=ROLE_CHOICES)

    def __str__(self):
        return self.role


class User(AbstractUser):

    role = models.ForeignKey(
        Role,
        on_delete=models.CASCADE,
        blank=True,
        null=True,
    )

    objects = UserManager()

    def __str__(self):
        return self.username


@receiver(models.signals.post_save, sender=User)
def add_role_to_user(sender, instance, **kwargs):
    print('instance in add role', instance)
    if kwargs.get('created'):
        try:
            role = Role.objects.get(user=instance)
            print('role', role)
        except Role.DoesNotExist:
            Role.objects.create(role="agent")
        try:
            token = Token.objects.get(user=instance)
            print('token', token)
        except Token.DoesNotExist:
            Token.objects.create(user=instance)
like image 983
milan Avatar asked Mar 06 '23 17:03

milan


1 Answers

Since you are not using email in create_superuser() you can add email as a keywork parameter and set to a None as

def create_superuser(self, username, password, email=None):
    """
    Creates and saves a superuser with the given username and password.
    """
    user = self.create_user(
        username=username,
        password=password,
    )
    user.is_superuser = True
    user.save(using=self._db)
    return user
like image 78
JPG Avatar answered Mar 12 '23 01:03

JPG