TypeError: user.models.CustomUser() получил несколько значений для ключевого аргумента 'email'
Итак, я пытаюсь создать пользовательскую модель пользователя в Django для личного проекта, но терминал выдает эту ошибку при попытке создать суперпользователя через 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'
Вот код managers.py;
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)
extra_fields['email'] = self.normalize_email('email')
user = self.model(email=email, **extra_fields)
Вы передаете email два раза в пользователя. Один раз email=email, а второй раз через **extra_fields. Вы можете передать его только один раз или изменить имя переменной второго email.