Jieke Wei
Jieke Wei

Reputation: 183

Django - create_superuser() got an unexpected keyword argument 'user_type'

I am trying to create a custom user model for my django app. Here is the code for CustomUser and CustomUserManager.

from django.utils.translation import ugettext_lazy as _

class CustomUserManager(BaseUserManager):
def _create_user(self, anonymous, first_name, last_name, email, username, password, home_address, user_type, image_path):
    now = timezone.now()

    if not email:
        raise ValueError('The gives email must be set')

    email = self.normalize_email(email)
    user = self.model(
        anonymous=anonymous,
        first_name=first_name,
        last_name=last_name,
        email=email,
        username=username,
        home_address=home_address,
        user_type=user_type,
        image_path=image_path,
        created_time=now,
        last_login=now
    )

    user.set_password(password)
    user.save(using=self._db)
    return user


    def create_superuser(self, first_name, last_name, email, username, password, home_address, image_path):
    return self._create_user(False, first_name, last_name, email, username, password, home_address, 0, image_path)

class CustomUser(AbstractBaseUser):
    anonymous = models.BooleanField()
    username = models.CharField(max_length=255, unique=True)
    first_name = models.CharField(max_length=255, blank=True)
    last_name = models.CharField(max_length=255, blank=True)
    email = models.EmailField(blank=True, unique=True)
    home_address = models.CharField(max_length=255, blank=True)
    user_type = models.IntegerField(1)
    image_path = models.CharField(max_length=500, blank=True)
    created_time = models.TimeField()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username', 'home_address', 'first_name', 'last_name', 'user_type']

    objects = CustomUserManager()
    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')

Then I get the error about unexpected argument user_type

Traceback (most recent call last):

File "manage.py", line 22, in execute_from_command_line(sys.argv)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management__init__.py", line 363, in execute_ from_command_line utility.execute()

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 283, in run_from_arg v self.execute(*args, **cmd_options)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py ", line 63, in execute return super(Command, self).execute(*args, **options)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\core\management\base.py", line 330, in execute output = self.handle(*args, **options)

File "C:\Users\Nutzer\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py ", line 183, in handle self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)

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

Thank in advance if anybody can help!

Upvotes: 0

Views: 2194

Answers (2)

Oswald Tragger
Oswald Tragger

Reputation: 31

Hi just run into the same issue, and taking a look at your code I realise an error. The return statement of your create_superuser() method should be :

return self.create_user(False, first_name, last_name, email, username, password, home_address, 0, image_path), 

instead of:

return self._create_user(False, first_name, last_name, email, username, password, home_address, 0, image_path)

Kindly lose the _ Hope this helps

Upvotes: 0

MrName
MrName

Reputation: 2529

I don't see where you ever assign the custom manager to your custom model. This is indicated by the fact that it seems to be looking at the default manager in the stack trace. Try assigning your custom manager to the model as described in the docs.

Upvotes: 1

Related Questions