亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法登錄在django管理員后端創建的超級用戶

無法登錄在django管理員后端創建的超級用戶

慕田峪7331174 2021-04-10 16:15:52
我正在嘗試在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()這是相關的后端設置。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 FrameworkREST_FRAMEWORK = {    'DEFAULT_AUTHENTICATION_CLASSES': (        'oauth2_provider.contrib.rest_framework.OAuth2Authentication',    ),    'DEFAULT_PERMISSION_CLASSES': (        'rest_framework.permissions.IsAuthenticated',    ),}我正在檢查創建表單上的is_staff和is_superuser是否正確,仍然一無所獲。創建的超級用戶無法登錄到管理后端。我在這里做錯什么了。
查看完整描述

2 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

首先,您正在使用自定義用戶模型。在您的設置文件中,您是否告訴django使用您的模型而不是默認的auth.models.User?


第二,


您正在create_superuser函數中調用self.create_user。


您是否也覆蓋create_user函數?


如果是,請提供實施方案。


更新


您不需要像以前那樣更改實現用戶管理器的功能。如果您在django.auth.models中查看UserManger的實現。您可以從那里獲得靈感,并且避免犯錯-例如:創建標準用戶時不保存模型,因為您不調用保存方法。


我建議像這樣更改您的UserManager:


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)


查看完整回答
反對 回復 2021-04-20
  • 2 回答
  • 0 關注
  • 612 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號