在Django中更新配置文件时,在用户模型上允许空白密码字段 [英] Allow blank password fields on User model when updating profile in Django

查看:83
本文介绍了在Django中更新配置文件时,在用户模型上允许空白密码字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class UserForm(UserCreationForm):

    def __init__(self, *arg, **kw):
        super(UserForm, self).__init__(*arg, **kw)

        # re-order so email appears last on this form
        email_field = self.fields.pop('email')
        self.fields['email'] = email_field


    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'email')






我有一个表格,用户可以更新其个人资料详细信息。


I have a form were users can update their profile details.

用户可以选择更新其密码,但这不是更新其他配置文件字段所必需的。

Users can optionally update their passwords but it's not required in order to update other profile fields.

问题是当密码确认字段为空白。

用户模型密码字段,我认为这是 required = True blank = False 字段,因此我需要一种在此模型上进行验证时忽略表单空白输入的方法。

The User models Password field I believe is a required=True and blank=False field so I need a way of simply ignoring blank input from forms when validating on this model.

User 模型是Django附带的模型:

The User model is the one that ships with Django:

。 contrib.auth.models import User

谢谢

推荐答案

UserCreationForm 用于创建

class UserUpdateForm(forms.ModelForm):
    # Feel free to add the password validation field as on UserCreationForm
    password = forms.CharField(required=False, widget=forms.PasswordInput)

    class Meta:
        model = User
        # Add all the fields you want a user to change
        fields = ('first_name', 'last_name', 'username', 'email', 'password') 

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

或者如果您想将 UserCreationForm 子类化,我不建议这样做。您可以这样做:

Or if you want to subclass the UserCreationForm which I doesn't recommend. You can do this :

class UserForm(UserCreationForm):
    password1 = forms.CharField(label=_("Password"), required=False
                            widget=forms.PasswordInput)
    password2 = forms.CharField(label=_("Password confirmation"),
                            widget=forms.PasswordInput, required=False)
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'email')

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

我建议您使用简单的

class UserUpdateForm(forms.ModelForm):       
    class Meta:
        model = User
        # Add all the fields you want a user to change
        fields = ('first_name', 'last_name', 'username', 'email') 

对于更改密码,您可以使用另一种专用形式,它是 django.contrib.auth.forms.SetPasswordForm ,因为更改了密码是与更新用户信息不同的过程

For passwords changing there's another dedicated form that your can use which is django.contrib.auth.forms.SetPasswordForm since changing the password is a different process from updating user informations

这篇关于在Django中更新配置文件时,在用户模型上允许空白密码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆