无法登录在 django 管理后端创建的超级用户



我正在尝试在django管理后端创建超级用户,但不知何故我无法让他们登录。 这是我的用户类,

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()

这是创建超级用户的用户管理器函数,

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_staffis_superuser,仍然一无所获。创建的超级用户无法登录管理员后端。 我在这里做错了什么。

首先, 您正在使用自定义用户模型。在你的设置文件中,你是否告诉 django 使用你的模型而不是默认的 auth.models.User ?

第二

您正在create_superuser函数中调用self.create_user。

你也覆盖create_user函数吗?

如果是,请提供实施情况。

更新

您不需要像以前那样更改实现用户管理的功能。 如果你看看 userManger 在 django.auth.models 中的实现。您可以从那里获得灵感,避免犯错误 - 例如:创建标准用户时不会保存模型,因为您不调用 save 方法。

我建议像这样更改您的用户管理器:

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)

我用你的代码创建了一个新项目,但我无法重现你的问题。我这边一切都很好。但是,我确实注意到有关您的代码的一些事情:

首先,在您的settings.py中,确保您已将AUTH_USER_MODEL设置为your_app.User

第二,在你的UserManager课上,我发现了这样的事情:

@property
def is_staff(self):
return self.is_staff

这对我来说似乎是一个错误,因为它会产生无限递归。我想在你的情况下,django 的代码中有一个包罗万象的子句,所以递归错误被静音了。这很可能是问题所在。

最新更新