2019-04-03 User模型

重写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模型)