【问题标题】:Django 1.5: UserCreationForm & Custom Auth ModelDjango 1.5:UserCreationForm 和自定义身份验证模型
【发布时间】:2013-05-09 21:01:46
【问题描述】:

我正在使用 Django 1.5 和 Python 3.2.3。

我有一个自定义身份验证设置,它使用电子邮件地址而不是用户名。模型中根本没有定义用户名。这很好用。然而,当我构建一个用户创建表单时,它无论如何都会添加一个用户名字段。所以我尝试准确定义我想要显示的字段,但它仍然强制用户名字段进入表单...... 即使它甚至不存在于自定义身份验证模型中。我怎样才能让它停止这样做?

我的表单是这样定义的:

class UserCreateForm(UserCreationForm):

    class Meta:
        model = MyUsr
        fields = ('email','fname','surname','password1','password2',
                  'activation_code','is_active')

在文档中,Custom Users and Builtin Forms 表示“必须为任何自定义用户模型重新编写”。我认为这就是我在这里所做的。不过,无论是 this 还是 UserCreationForm documentation 都没有对此多说什么。所以我不知道我错过了什么。我也没有通过 Google 找到任何东西。

【问题讨论】:

    标签: django django-models python-3.x django-forms


    【解决方案1】:

    您的UserCreationForm 应该类似于

    # forms.py
    from .models import CustomUser
    
    class UserCreationForm(forms.ModelForm):
        password1 = forms.CharField(label="Password", widget=forms.PasswordInput)
        password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
    
        class Meta:
            model = CustomUserModel
            # Note - include all *required* CustomUser fields here,
            # but don't need to include password1 and password2 as they are
            # already included since they are defined above.
            fields = ("email",)
    
        def clean_password2(self):
            # Check that the two password entries match
            password1 = self.cleaned_data.get("password1")
            password2 = self.cleaned_data.get("password2")
            if password1 and password2 and password1 != password2:
                msg = "Passwords don't match"
                raise forms.ValidationError("Password mismatch")
            return password2
    
        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
    

    您还需要一个不会覆盖密码字段的用户更改表单:

    class UserChangeForm(forms.ModelForm):
        password = ReadOnlyPasswordHashField()
    
        class Meta:
            model = CustomUser
    
        def clean_password(self):
            # always return the initial value
            return self.initial['password']
    

    像这样在您的管理员中定义这些:

    #admin.py
    
    from .forms import UserChangeForm, UserAddForm
    
    class CustomUserAdmin(UserAdmin):
        add_form = UserCreationForm
        form = UserChangeForm
    

    您还需要覆盖 list_displaylist_filtersearch_fieldsorderingfilter_horizontalfieldsetsadd_fieldsetsdjango.contrib.auth.admin.UserAdmin 中提到 username 的所有内容,我想我列出了所有这些)。

    【讨论】:

    • 保存方法中的两个勺子看起来很有趣:)
    • 我在哪里可以找到这个 TwoScoopsUserCreationForm ?
    【解决方案2】:

    您需要从 sctratch 创建表单,它不应扩展 UserCreationForm。 UserCreationForm 具有明确定义的用户名字段以及其他一些字段。你可以看看here

    【讨论】:

    • 呃,尽管 Django 有很多好处,但这是一个疯狂的坏点。它们允许在没有用户名的情况下进行自定义身份验证,但这会破坏 UserCreationForm。谢谢。至少我知道我没有做错什么来得到我得到的结果。
    • 但是文档说应该重写这个表单。而重写并不意味着从它延伸,它意味着一个新的
    猜你喜欢
    • 2021-01-09
    • 2013-03-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-17
    • 2016-10-07
    • 2021-02-06
    • 1970-01-01
    相关资源
    最近更新 更多