无法登录在django管理员后端创建的超级用户 [英] Can't log in super users created on the django admin backend
问题描述
我正在尝试在django管理员后端创建超级用户,但不知何故我无法让他们登录。
这是我的用户类,
I'm trying to create superusers on the django admin backend, but somehow I can't get them to log in. Here's my user class,
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True, max_length=255)
mobile = PhoneNumberField(null=True)
username = models.CharField(null=False, unique=True, max_length=255)
full_name = models.CharField(max_length=255, blank=True, null=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
objects = UserManager()
这里是UserManager函数,用于创建超级用户,
Here's the UserManager function to create super user,
class UserManager(BaseUserManager):
def create_user(self, email, mobile=None, username=None, full_name=None, gender=None, birthday=None, password=None,
is_staff=False,
is_superuser=False, is_active=False, is_mobile_verified=False, is_bot=False, is_online=False,
is_logged_in=True):
if not email:
raise ValueError("Can't create User without a mobile number!")
if not password:
raise ValueError("Can't create User without a password!")
user = self.model(
email=self.normalize_email(email),
mobile=mobile,
username=username,
full_name=full_name,
gender=gender,
birthday=birthday,
is_staff=is_staff,
is_superuser=is_superuser,
is_active=is_active,
)
user.set_password(password)
return user
def create_superuser(self, email, username, password=None):
user = self.create_user(
email,
username=username,
password=password,
is_staff=True,
is_superuser=True,
is_active=True,
)
user.save(self._db)
return user
@property
def is_superuser(self):
return self.is_superuser
@property
def is_staff(self):
return self.is_staff
@property
def is_active(self):
return self.is_active
@property
def is_mobile_verified(self):
return self.is_mobile_verified
def has_perm(self, perm, obj=None):
return self.is_staff or self.is_superuser
def has_module_perms(self, app_label):
return self.is_staff or self.is_superuser
@is_staff.setter
def is_staff(self, value):
self._is_staff = value
@is_superuser.setter
def is_superuser(self, value):
self._is_superuser = value
@is_active.setter
def is_active(self, value):
self._is_active = value
相关后端设置。
OAUTH2_PROVIDER = {
# this is the list of available scopes
'ACCESS_TOKEN_EXPIRE_SECONDS': 60 * 60 * 24,
'SCOPES': {'read': 'Read scope', 'write': 'Write scope', 'groups': 'Access to your groups'},
'OAUTH2_BACKEND_CLASS': 'oauth2_provider.oauth2_backends.JSONOAuthLibCore',
}
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
)
# REST Framework
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}
我是在创建表单上检查 is_staff
和 is_superuser
是否为true,仍然没有。创建的超级用户无法登录到管理员后端。
我在这里做什么错了。
I'm checking the is_staff
and is_superuser
to true on the creation form, still nothing. The created super user can't log in on the admin backend.
What am I doing wrong here.
推荐答案
首先,
使用的是自定义用户模型。在设置文件中,您是否告诉django使用您的模型而不是默认的auth.models.User?
First, you are using custom user model. In your settings file, do you tell django to use your model instead of default auth.models.User ?
第二,
您正在create_superuser函数中调用self.create_user。
you are calling self.create_user inside your create_superuser function.
是否也覆盖了create_user函数?
Do you override the create_user function as well ?
如果是,请提供实现。
更新
您不需要像以前那样更改实现用户管理器的功能。
如果您在django.auth.models中查看UserManger的实现。您可以从那里得到启发,并且避免犯错-例如:创建标准用户时不要保存模型,因为您不调用保存方法。
You dont need to change implementation user mansger's functions as you did. IF you look at implementation of UserManger in django.auth.models. You can take inspiration from there and you avoid making a mistake - for example: you dont save model when you create standard user as you dont call save method.
我建议像这样更改您的UserManager:
I suggest to change your UserManager like this:
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password,
**extra_fields):
if not email:
raise ValueError('The given email must be set')
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_user(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
extra_fields.setdefault('is_active', False)
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)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, password, **extra_fields)
这篇关于无法登录在django管理员后端创建的超级用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!