Django、ModelForms、用户和用户配置文件-不哈希密码

Django、ModelForms、用户和用户配置文件-不哈希密码,django,django-forms,Django,Django Forms,我正在尝试设置用户-用户配置文件关系,显示表单并保存数据 提交时,数据被保存,除了密码字段没有散列之外 Forms.py class UserForm(forms.ModelForm): username = forms.RegexField(label="Username", max_length=30, regex=r'^[\w.@+-]+$', help_text = "My text", error_messages = {'invalid':

我正在尝试设置用户-用户配置文件关系,显示表单并保存数据

提交时,数据被保存,除了密码字段没有散列之外

Forms.py

class UserForm(forms.ModelForm):
    username = forms.RegexField(label="Username", max_length=30,
         regex=r'^[\w.@+-]+$', help_text = "My text",
         error_messages = {'invalid':
           "This value may contain only letters, numbers and @/./+/-/_ characters."
         }
    )
    password = forms.CharField(label="Password",
                              widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ["first_name", "last_name", "username",  "email", "password"]

    def clean_username(self):
        username = self.cleaned_data['username']
        if not re.search(r'^\w+$', username):
            raise forms.ValidationError(
                  'Username can contain only alphanumeric characters')
        try:
            User.objects.get(username=username)
        except ObjectDoesNotExist:
            return username
        raise forms.ValidationError('Username is already taken')

class UserProfileForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['user_is']

编辑:在写下此答案后,对原始问题进行了编辑

要为用户设置密码,您不需要设置
profile.user.password=new\u password
——在本例中使用modelform就是这样做的;这将直接将其设置为未清除的值

您需要使用适当的API来设置密码。因此,在
profile.save()
put之前:

profile.user.set_密码(uform.cleaned_数据['password'])


要删除帮助文本,请不要使用quick form.as_foo渲染器,或者在ModelForm的init()方法(请参阅Django forms文档)中重写字段以使帮助文本为none,好的,回答我自己的问题。这可能对其他人有用

将以下内容添加到
UserForm

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