我有自己的自定义用户模型,也有自己的Manger.
楷模:
class MyUser(AbstractBaseUser,PermissionsMixin): email = models.EmailField(max_length=255,unique=True) first_name = models.CharField(max_length=35) last_name = models.CharField(max_length=35) username = models.CharField(max_length=70,unique=True) date_of_birth = models.DateField() is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) @property def is_staff(self): return self.is_admin def get_full_name(self): return ('%s %s') % (self.first_name,self.last_name) def get_short_name(self): return self.username objects = MyUserManager() USERNAME_FIELD = 'email' required_FIELDS = ['first_name','last_name','username','date_of_birth']
经理:
class MyUserManager(BaseUserManager): def create_user(self,email,first_name,last_name,username,date_of_birth,password=None,**kwargs): if not email: raise ValueError('User must have an email address') user = self.model( email=self.normalize_email(email),first_name=first_name,last_name=last_name,username=username,date_of_birth=date_of_birth,**kwargs ) user.set_password(self.cleaned_data["password"]) user.save(using=self._db) return user def create_superuser(self,password,**kwargs): user = self.create_user( email,password=password,is_superuser=True,**kwargs ) user.is_admin = True user.save(using=self._db) return user
创建新用户时没有任何错误,一切正常.但是当我尝试登录时我不能.所以我检查了用户的密码进行确认,密码显示为纯文本strongpassword,当更改管理表单以使用ReadOnlyPasswordHashField获取散列密码时,我在密码字段中出现错误,即使我在内部使用了set_password() create_user()函数.
Invalid password format or unknown hashing algorithm
但是,如果我为该用户手动执行set_password(‘strongpassword’),则会对其进行哈希处理.你能帮我解决这个问题吗?谢谢.