在 Django (django-rest-framework) 中创建超级用户时出现致命错误
Fatal error while creating superuser in Django (django-rest-framework)
我想创建一个没有用户名字段的 AbstractUser,所以我得到了这个:
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
class CustomUser(AbstractUser):
email = models.EmailField(max_length=254, unique=True)
username = None
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
session_token = models.CharField(max_length=10, default=0)
#Newsletter/Shipment Info (not required)
newsletter = models.BooleanField(default=False)
first_name = models.CharField(max_length=35, blank=True, null=True)
last_name = models.CharField(max_length=35, blank=True, null=True)
adress = models.CharField(max_length=50, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
postal_code = models.CharField(max_length=6, blank=True, null=True)
phone = models.CharField(max_length=9, validators=[RegexValidator(r'^\d{9}$/')], blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
我已将其迁移到数据库中,现在我需要创建一个管理员用户,这样我才能进入管理面板。我在 python manage.py createsuperuser
之后在命令提示符中输入了电子邮件和两个密码,但出现了这个错误:
Traceback (most recent call last):
File "C:\Users\mateu\Documents\meadow\backend\manage.py", line 22, in <module>
main()
File "C:\Users\mateu\Documents\meadow\backend\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\__init__.py", line 425, in execute_from_command_line
utility.execute()
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\base.py", line 417, in execute
output = self.handle(*args, **options)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 195, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'username'
我不知道如何修复它。有什么想法吗?
您必须从 AbstractBaseUser
而不是 AbstractUser
继承并向其添加 Manager Class
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=255, unique=True)
is_superuser = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
# Add other fields here
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class UserManager(BaseUserManager):
def _create_user(self, email, password, **extra_fields):
user = self.model(email=self.normalize_email(email), **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password, **extra_fields):
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self._create_user(email, password, **extra_fields)
别忘了
python manage.py makemigrations
python manage.py migrate
我建议你看看docs
我想创建一个没有用户名字段的 AbstractUser,所以我得到了这个:
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
class CustomUser(AbstractUser):
email = models.EmailField(max_length=254, unique=True)
username = None
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
session_token = models.CharField(max_length=10, default=0)
#Newsletter/Shipment Info (not required)
newsletter = models.BooleanField(default=False)
first_name = models.CharField(max_length=35, blank=True, null=True)
last_name = models.CharField(max_length=35, blank=True, null=True)
adress = models.CharField(max_length=50, blank=True, null=True)
city = models.CharField(max_length=50, blank=True, null=True)
postal_code = models.CharField(max_length=6, blank=True, null=True)
phone = models.CharField(max_length=9, validators=[RegexValidator(r'^\d{9}$/')], blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
我已将其迁移到数据库中,现在我需要创建一个管理员用户,这样我才能进入管理面板。我在 python manage.py createsuperuser
之后在命令提示符中输入了电子邮件和两个密码,但出现了这个错误:
Traceback (most recent call last):
File "C:\Users\mateu\Documents\meadow\backend\manage.py", line 22, in <module>
main()
File "C:\Users\mateu\Documents\meadow\backend\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\__init__.py", line 425, in execute_from_command_line
utility.execute()
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\__init__.py", line 419, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\base.py", line 373, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\core\management\base.py", line 417, in execute
output = self.handle(*args, **options)
File "C:\Users\mateu\Documents\meadow\env\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 195, in handle
self.UserModel._default_manager.db_manager(database).create_superuser(**user_data)
TypeError: create_superuser() missing 1 required positional argument: 'username'
我不知道如何修复它。有什么想法吗?
您必须从 AbstractBaseUser
而不是 AbstractUser
继承并向其添加 Manager Class
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.models import PermissionsMixin
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=255, unique=True)
is_superuser = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
# Add other fields here
objects = UserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class UserManager(BaseUserManager):
def _create_user(self, email, password, **extra_fields):
user = self.model(email=self.normalize_email(email), **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password, **extra_fields):
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
return self._create_user(email, password, **extra_fields)
别忘了
python manage.py makemigrations
python manage.py migrate
我建议你看看docs