我在Django中启用了用户auth模块,但是当我使用UserCreationForm
时,他只询问用户名和两个密码/密码确认字段。我还想要电子邮件和全名字段,并设置为必填字段。
我做到了这一点:
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
fullname = forms.CharField(label = "Full name")
class Meta:
model = User
fields = ("username", "fullname", "email", )
现在表单显示新字段,但不会将它们保存在数据库中。
我该如何解决?
答案 0 :(得分:19)
用户模型中没有名为fullname
的字段。
如果您希望使用原始模型存储名称,则必须将其作为名字和姓氏单独存储。
修改:如果您只想在表单中使用一个字段并仍然使用原始用户模型,请使用以下内容:
您可以这样做:
from django.contrib.auth.forms import UserCreationForm
from django import forms
from django.contrib.auth.models import User
class RegisterForm(UserCreationForm):
email = forms.EmailField(label = "Email")
fullname = forms.CharField(label = "First name")
class Meta:
model = User
fields = ("username", "fullname", "email", )
现在你必须做manji所说的并覆盖save方法,但是由于User模型没有fullname字段,它应该如下所示:
def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
first_name, last_name = self.cleaned_data["fullname"].split()
user.first_name = first_name
user.last_name = last_name
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
注意:您应该为fullname字段添加一个clean方法,以确保输入的fullname只包含两个部分,第一个名称和姓氏,以及它具有其他有效字符。
用户模型的参考源代码:
http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py#L201
答案 1 :(得分:4)
您必须覆盖UserCreationForm.save()
方法:
def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
user.fullname = self.cleaned_data["fullname"]
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user
http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/forms.py#L10
答案 2 :(得分:4)
我刚刚在StackOverflow上注册了一个帐户,因此缺少直接回复chands答案所需的积分。但是,请注意以下代码行:
first_name, last_name = self.cleaned_data["fullname"].split()
如果fullname类似于“Foo Bar Baz”(例如,如果您的用户具有中间名,则为两个空格实例),这会引发“ValueError:解压缩的值太多”。我使用了一个轻微的变化,它将初始空格后的所有内容填充到last_name中。
first_name, last_name = self.cleaned_data["fullname"].split(None, 1)
答案 3 :(得分:3)
在 django 1.10 中,这是我在admin.py中添加 first_name ,电子邮件和 last_name 的内容到默认的django用户创建表单
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from django.contrib import admin
from django.contrib.auth.models import Group, User
# first unregister the existing useradmin...
admin.site.unregister(User)
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
# The fields to be used in displaying the User model.
# These override the definitions on the base UserAdmin
# that reference specific fields on auth.User.
list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
fieldsets = (
(None, {'fields': ('username', 'password')}),
('Personal info', {'fields': ('first_name', 'last_name', 'email',)}),
('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}),
('Important dates', {'fields': ('last_login', 'date_joined')}),)
# add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
# overrides get_fieldsets to use this attribute when creating a user.
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'first_name', 'last_name', 'password1', 'password2')}),)
list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
search_fields = ('username', 'first_name', 'last_name', 'email')
ordering = ('username',)
filter_horizontal = ('groups', 'user_permissions',)
# Now register the new UserAdmin...
admin.site.register(User, UserAdmin)