修改Django UserCreationForm [英] Modifying Django UserCreationForm

查看:482
本文介绍了修改Django UserCreationForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为标准的Django UserCreationForm添加更多的字段,所以我继续将其分类到我的应用程序的forms.py文件中,最后是这样的:

  class CustomUserCreationForm(UserCreationForm):
email = forms.EmailField(label =Email)
first_name = forms.CharField(label =First name)
last_name = forms.CharField(label =Last name)

class Meta:
model =用户
fields =(first_name,last_name,username $)

def save(self,commit = True):
user = super(CustomUserCreationForm,self).save(commit = False)
user.first_name = first_name
user.last_name = last_name
user.email = self.cleaned_data [email]
如果提交:
user.save()
返回用户

然后,我为用户创建了一个自定义的ModelAdmin,如下所示:

  class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
inlines = [ProfileInline,]


admin.site.unregister(User)
admin.site.register(User,CustomUserAdmin)
admin.site.register(Class,ClassAdmin)

然而,无论我做什么,当我进入管理页面并尝试添加新用户时,新的CustomUserCreationForm不会显示。我在哪里拧紧?



编辑:表单似乎没有显示,但正在被使用。如果我尝试添加一个新的用户只使用典型的UserCreationForm的用户名和密码字段,我收到一个错误,当我删除我的ModelAdmin中的add_form行时,它会立即消失。为什么不显示?



我的本​​地应用程序目录中还没有任何管理模板。这可能是问题吗?

解决方案

您需要将您添加的字段添加到自定义的add_fieldsets属性中用户管理类。

  #example:
add_fieldsets =(
(无,{
'classes':('wide',),
'fields':('username','email','password1','password2')}
),

用户在第二天有类似的问题:我如何将Django用户注册一步(而不是两步)涉及创建自定义用户管理表单的过程与邮件强制?


I wanted to add more fields to the standard Django UserCreationForm so I went ahead and subclassed it inside of my app's forms.py file and ended up with this:

class CustomUserCreationForm(UserCreationForm):
    email = forms.EmailField(label = "Email")
    first_name = forms.CharField(label = "First name")
    last_name = forms.CharField(label = "Last name")

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

    def save(self, commit=True):
        user = super(CustomUserCreationForm, self).save(commit=False)
        user.first_name = first_name
        user.last_name = last_name
        user.email = self.cleaned_data["email"]
        if commit:
            user.save()
        return user

I then went ahead and created a custom ModelAdmin for my Users, which looks like this:

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    inlines = [ProfileInline,]


admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)
admin.site.register(Class, ClassAdmin)

No matter what I do, however, the new CustomUserCreationForm does not show up when I go into the Admin page and try to add a new user. Where am I screwing up?

EDIT: It seems that the form is not being displayed, but is being used. If I try to add a new user using just the username and password fields that the typical UserCreationForm has, I get an error, which promptly goes away when I remove the add_form line in my ModelAdmin. Why is it not displaying?

I also do not have any admin templates in my local app directory. Could this be the issue?

解决方案

You'll need to add the fields you've added to the add_fieldsets property of your custom user admin class.

#example:
add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email', 'password1', 'password2')}
        ),
    )

A user had a similar question the other day: How can I have Django user registration single step (instead of two step)process with email compulsory? which involved creating a custom user admin form as well.

这篇关于修改Django UserCreationForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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