美文网首页
2019-04-03 User模型

2019-04-03 User模型

作者: 杨柳_Papaya | 来源:发表于2019-04-03 08:14 被阅读0次

    重写django User 模型

    class UserManager(BaseUserManager):
    
        def _create_user(self,email,username,password,**kwargs):
            if not email:
                raise ValueError('请输入邮箱地址!')
            if not username:
                raise ValueError('请输入用户名!')
            if not password:
                raise ValueError('请输入登录密码!')
            user = self.model(email=email,username=username,**kwargs)
            user.set_password(password)
            user.save()
            return user
    
        def create_user(self,email,username,password,**kwargs):
            kwargs['is_superuser'] = False
            return self._create_user(email,username,password,**kwargs)
    
        def create_superuser(self,email,username,password,**kwargs):
            kwargs['is_superuser'] = True
            return self._create_user(email, username, password, **kwargs)
    
    class User(AbstractBaseUser,PermissionsMixin):
        #主键使用shortuuid
        uid = ShortUUIDField(primary_key=True)
        email = models.EmailField(unique=True,max_length=100)
        # password = models.CharField(max_length=20)
        username = models.CharField(max_length=100)
        is_active = models.BooleanField(default=True)
        is_staff = models.BooleanField(default=False)
        data_joined = models.DateTimeField(auto_now_add=True)
    
        USERNAME_FIELD = 'email'
        REQUIRED_FIELDS = ['username']
        EMAIL_FIELD = 'email'
    
        objects = UserManager()
    
        def get_full_name(self):
            return self.username
    
        def get_short_name(self):
            return self.username
    

    相关文章

      网友评论

          本文标题:2019-04-03 User模型

          本文链接:https://www.haomeiwen.com/subject/wegobqtx.html