0
votes

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!

1
Did you set this custom model as AUTH_USER_MODEL in the settings?schwobaseggl
@schwobaseggl yes, I did AUTH_USER_MODEL = 'CustomUser.CustomUser'. The first CustomUser is an applicationJieke Wei

1 Answers

1
votes

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.