xec86
xec86

Reputation: 1421

create_superuser for custom authentication model

I have created the following code to build a custom Django authentication model and I am attempting to create a superuser with the custom user manager but when I attempt to use the python manage.py createsuperuser command, I receive the following error: TypeError: create_superuser() got an unexpected keyword argument 'password'

UserHandler/models.py

# Custom Auth Manager
class AuthManager(BaseUserManager):
    def create_superuser(self, supplied_email, supplied_password, supplied_first_name, supplied_last_name, supplied_language):
        # Make this password great again (w/ salting and hashing)
        hashed_salted_password = AbstractBaseUser.set_password(supplied_password)
        superuser = AuthLookup()
        superuser.email_address = supplied_email
        superuser.password = hashed_salted_password
        superuser.first_name = supplied_first_name
        superuser.last_name = supplied_last_name
        superuser.language = supplied_language
        superuser.save()


# Abstract Auth Model
class AuthLookup(AbstractBaseUser, PermissionsMixin):
    email_address = models.EmailField(unique=True)
    password = models.CharField(max_length=100)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    organization_id = models.UUIDField(null=False, max_length=36, default=uuid.uuid4)
    # Firebase tokens(Max length represents 5 device tokens at maximum length of 256 characters plus 4 commas.)
    associated_devices = models.CharField(max_length=1284)
    is_requester = models.BooleanField(null=False, default=0)
    is_responder = models.BooleanField(null=False, default=0)
    user_identifier = models.UUIDField(null=False, max_length=36, default=uuid.uuid4)
    language = models.CharField(max_length=6, default='US_EN')
    user_added = models.DateTimeField(default='1970-01-01 00:00:00')

    object = AuthManager()

    USERNAME_FIELD = 'email_address'
    REQUIRED_FIELDS = ['first_name', 'last_name', 'language']

Settings.py

AUTH_USER_MODEL = 'UserHandler.AuthLookup'

Upvotes: 0

Views: 157

Answers (1)

xec86
xec86

Reputation: 1421

Turns out that the arguments passed to the create_superuser functions must be named the same as the fields in the model.

The error is not present now. (Ive moved on to different errors unfortunately)

class AuthManager(BaseUserManager):
def create_superuser(self, email_address, password, first_name, last_name, language):
    # Keep everyone honest here, make sure the email is valid
    email_address = self.normalize_email(email_address)
    # Make this password great again (w/ salting and hashing)
    password = AbstractBaseUser.set_password(password)
    superuser = AuthLookup()
    superuser.email_address = email
    superuser.password = password
    superuser.first_name = first_name
    superuser.last_name = last_name
    superuser.language = language
    superuser.save(using=self._db)

Upvotes: 1

Related Questions