简体   繁体   English

扩展Django的UserCreationForm

[英]Extendings Django's UserCreationForm

I'm trying to extend Django's django.contrib.auth.forms.UserCreationForm to include email and name fields. 我正在尝试扩展Django的django.contrib.auth.forms.UserCreationForm以包括电子邮件和名称字段。 My similar extension of the UserChangeForm works fine, but UserCreationForm still only shows the default username, password1, and password2 fields. 我类似的UserChangeForm扩展名可以正常工作,但UserCreationForm仍仅显示默认的用户名,password1和password2字段。 Any ideas what I'm doing wrong? 有什么想法我做错了吗?

forms.py forms.py

class AuthorCreation(UserCreationForm):

    class Meta:
        model = User
        fields = ('username',  'password1', 'password2',
                  'first_name', 'last_name', 'email', 'groups', 'is_staff')


class AuthorChange(UserChangeForm):

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

admin.py admin.py

class AuthorAdmin(UserAdmin):
    """Admin class for creating and managing Authors"""
    inlines = [AuthorInline]

    fieldsets = (
        (None, {
            'fields': ('username', ('first_name', 'last_name'),
                       'email', 'password', 'groups', 'is_staff')
        }),
    )

    class Media:
        css = {
            'all': [
                'css/codenotes-admin.css',
            ]
        }

    def get_form(self, request, obj=None, **kwargs):
        if obj is None:
            return forms.AuthorCreation
        else:
            return forms.AuthorChange


admin.site.unregister(User)
admin.site.register(User, AuthorAdmin)

Again, the AuthorChangeForm correctly displays all the fields, but AuthorCreationForm only displays the username, password1, and password2 fields (plus the inline forms, which works fine on both). 再次,AuthorChangeForm正确显示所有字段,但AuthorCreationForm仅显示用户名,password1和password2字段(加上内联表单,这两种方法都可以正常使用)。

I assume the problem is with the fieldsets, but I can't figure it out from the docs. 我认为问题出在字段集上,但是我无法从文档中找出问题所在。

I figured out the solution. 我想出了解决方案。 The problem was, as I suspected, the fieldsets. 正如我所怀疑的那样,问题出在字段集上。 It turns out that the UserAdmin class inherits has TWO fieldsets that need to be overridden, fieldsets and add_fieldsets . 事实证明,在UserAdmin类继承有两个字段集需要被重写, fieldsetsadd_fieldsets I didn't see this mentioned anywhere in the docs (maybe I just missed it); 我在文档中的任何地方都没有看到此内容(也许我只是错过了它)。 I had to dig through the source to find it. 我必须仔细研究源才能找到它。 As the name suggests, add_fieldsets is used when adding a new user. 顾名思义,添加新用户时将使用add_fieldsets The following admin class works as expected: 以下管理类按预期工作:

admin.py admin.py

class AuthorAdmin(UserAdmin):
    """Admin class for creating and managing Authors"""
    inlines = [AuthorInline]

    fieldsets = (
        (None, {
            'fields': ('username', ('first_name', 'last_name'),
                       'email', 'password', 'groups', 'is_staff')
        }),
    )
    add_fieldsets = (
        (None, {
            'fields': ('username', 'password1', 'password2',
                       ('first_name', 'last_name'),
                       'email', 'groups', 'is_staff'),
        }),
    )

    class Media:
        css = {
            'all': [
                'css/codenotes-admin.css',
            ]
        }

    def get_form(self, request, obj=None, **kwargs):
        if obj is None:
            return forms.AuthorCreation
        else:
            return forms.AuthorChange


admin.site.unregister(User)
admin.site.register(User, AuthorAdmin)

From the Django doc: 从Django文档中:

class UserCreationForm¶ A ModelForm for creating a new user. classUserCreationForm¶用于创建新用户的ModelForm。

It has three fields: username (from the user model), password1, and password2. 它具有三个字段:用户名(来自用户模型),密码1和密码2。 It verifies that password1 and password2 match, validates the password using validate_password(), and sets the user's password using set_password(). 它验证password1和password2匹配,使用validate_password()验证密码,并使用set_password()设置用户密码。

So it seems you have to create the extra fields yourself like this: 因此,看来您必须像这样自己创建额外的字段:

class AuthorCreation(UserCreationForm):
    first_name = forms.CharField(required=True, label="First Name")
    ... etc ...

    class Meta:
        model = User
        fields = ('username',  'password1', 'password2',
                  'first_name', 'last_name', 'email', 'groups', 'is_staff')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM