修改 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 = 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
然后我继续为我的用户创建一个自定义 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 行时,该错误会立即消失。为什么不显示?
我的本地应用程序目录中也没有任何管理模板。这可能是问题所在吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将已添加的字段添加到自定义用户管理类的 add_fieldsets 属性中。
前几天,一位用户提出了类似的问题: 如何让 Django 用户注册一步(而不是两步)过程并强制发送电子邮件? 其中涉及创建自定义用户管理表单: 出色地。
You'll need to add the fields you've added to the add_fieldsets property of your custom user admin class.
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.