哈希密码成为真实密码

时间:2017-03-23 22:59:08

标签: python django django-forms django-admin

当我尝试在django admin中更改用户的字段然后保存它时,哈希密码最终成为真正的密码。

因此,如果密码是像这样的pbkdf2adhfkhadqeqerqfavghhfyb进行哈希处理,并且我更改了用户模型中的另一个字段,则此哈希密码将成为我的非哈希密码。

我的代码是这样的。

class UserCreationForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ('Email','name','password','is_staff','is_superuser','Teacher',
                  'Student', 'Data_Joined', 'Is_active')

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password"])
        if commit:
            user.save()
        return user

class UserAdmin(admin.ModelAdmin):
    form = UserCreationForm

admin.site.register(User, UserAdmin)

1 个答案:

答案 0 :(得分:1)

我认为这是因为UserChangeForm默认值使用ReadOnlyPasswordHashField字段password

我会尝试:

class UserCreationForm(forms.ModelForm):

     password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ('Email','name','is_staff','is_superuser','Teacher',
              'Student', 'Data_Joined', 'Is_active')

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data["password1"])
        if commit:
            user.save()
    return user