Search code examples
pythondjangodjango-users

TypeError: user.models.CustomUser() got multiple values for keyword argument 'email'


So, I'm trying to create a custom user model in Django for a personal project, but the terminal is throwing this error while trying to create a super user via python3 manage.py createsuperuser

Traceback (most recent call last):
  File "/home/Henkan/main_store/manage.py", line 22, in <module>
    main()
  File "/home/Henkan/main_store/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 87, in execute
    return super().execute(*args, **options)
  File "/usr/local/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.10/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 232, in handle
    self.UserModel._default_manager.db_manager(database).create_superuser(
  File "/home/Henkan/main_store/user/managers.py", line 18, in create_superuser
    return self.create_user(email, password = None, **extra_fields)
  File "/home/Henkan/main_store/user/managers.py", line 9, in create_user
    user = self.model(email=email, **extra_fields)
TypeError: user.models.CustomUser() got multiple values for keyword argument 'email'

Here's the managers.py code;

from django.contrib.auth.base_user import BaseUserManager

class UserManager(BaseUserManager):
    def create_user(self, email, password = None, **extra_fields):
        if not email:
            raise ValueError('Email is required')

        extra_fields['email'] = self.normalize_email('email')
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self.db)

    def create_superuser(self, email, password = None, **extra_fields):
        extra_fields.setdefault('is_staff',True)
        extra_fields.setdefault('is_superuser',True)
        extra_fields.setdefault('is_active',True)

        return self.create_user(email, password = None, **extra_fields)

Solution

  • extra_fields['email'] = self.normalize_email('email')
    user = self.model(email=email, **extra_fields)
    

    You are passing email two times in user. One time email=email and second time through **extra_fields. You can pass it one time only or change the variable name of second email.