我'm creating new users using a custom UserCreationForm and then using password_reset from auth.views with a customized email template to send a signup mail to the user. This process is working fine, users are able to successfully complete the signup process and start using the app. I now want to create users without having to set a password for the user. I tried setting self.fields[' password1 '].required and self.fields[' password2'] . 在UserCreationForm初始化方法中要求为假,如推荐here,但是没有't worked for me. Unfortunately, it'证明难以调试 . 我被叫到了.2777881 .


class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ("email",)

    def __int__(self):
        self.fields['password1'].required = False
        self.fields['password2'].required = False

    def save(self, commit=True):
        user = super(UserCreateForm, self).save(commit=False)
        clean_email = self.cleaned_data["email"] = clean_email
        if commit:
        custom_user = CustomUser() = user
        return user


class UserRegistration(RegistrationView):
    def __init__(self):
        self.form_class = UserCreateForm
        self.success_url = 'login'
        self.template_name = 'register.html'

    def register(self, request, form):
        new_user =

我正在使用Django 1.8 . 如果有任何其他信息可用于分享,请告诉我 .


2 years ago

如您所述,您修改了表单字段以不需要密码,但该模型呢?内置 User 模型强制使用密码(它是必需的模型字段),如果您尝试保存没有密码的 User 对象,则会出错 . 有's a special method for when you don' t想要设置一个真正的密码 - set_unusable_password . 在保存表单数据之前,在视图中使用它 .