如何在我的 CustomUserCreationForm 中添加字段?
How can i add fields in my CustomUserCreationForm?
Forms.py
class CustomUserCreationForm(UserCreationForm):
#phone_number = forms.CharField(max_length = 100)
class Meta:
model = CustomUser
fields = ('username', 'email','first_name','last_name', 'phone_number', 'middle_name')
Admin.py
class CustomUserAdmin(UserAdmin):
model = CustomUser
add_form = CustomUserCreationForm
form = CustomUserChangeForm
fieldsets = (
(('User'), {'fields': ('username', 'email', 'phone_number', 'first_name', 'last_name', 'middle_name',
'is_staff', 'is_superuser', 'is_active', 'is_parent', 'is_teacher')}),
)
Models.py(Admin.py中的所有字段都在这里声明无误)
class CustomUser(AbstractUser):
phone_number = models.CharField(max_length = 100, blank=True, null = True)
first_name = models.CharField(max_length = 100, default = '')
last_name = models.CharField(max_length = 100, default = '')
AddNewUser Image Here
如您所见,我尝试直接在 CustomUserCreationForm 中添加一个 Charfield,我也尝试添加字段,但每当我添加新用户时它都不会出现。
这应该适合你
class CustomUserAdmin(UserAdmin):
model = CustomUser
add_form = CustomUserCreationForm
form = CustomUserChangeForm
fieldsets = (
(('None'), {'fields': ('username', 'email', 'phone_number', 'first_name', 'last_name', 'middle_name',
'is_staff', 'is_superuser', 'is_active', 'is_parent', 'is_teacher')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'phone_number', 'first_name', 'last_name', 'middle_name',
'is_staff', 'is_superuser', 'is_active', 'is_parent', 'is_teacher' ),
}),
)
admin.site.register(CustomUser, CustomUserAdmin),
Forms.py
class CustomUserCreationForm(UserCreationForm):
#phone_number = forms.CharField(max_length = 100)
class Meta:
model = CustomUser
fields = ('username', 'email','first_name','last_name', 'phone_number', 'middle_name')
Admin.py
class CustomUserAdmin(UserAdmin):
model = CustomUser
add_form = CustomUserCreationForm
form = CustomUserChangeForm
fieldsets = (
(('User'), {'fields': ('username', 'email', 'phone_number', 'first_name', 'last_name', 'middle_name',
'is_staff', 'is_superuser', 'is_active', 'is_parent', 'is_teacher')}),
)
Models.py(Admin.py中的所有字段都在这里声明无误)
class CustomUser(AbstractUser):
phone_number = models.CharField(max_length = 100, blank=True, null = True)
first_name = models.CharField(max_length = 100, default = '')
last_name = models.CharField(max_length = 100, default = '')
AddNewUser Image Here
如您所见,我尝试直接在 CustomUserCreationForm 中添加一个 Charfield,我也尝试添加字段,但每当我添加新用户时它都不会出现。
这应该适合你
class CustomUserAdmin(UserAdmin):
model = CustomUser
add_form = CustomUserCreationForm
form = CustomUserChangeForm
fieldsets = (
(('None'), {'fields': ('username', 'email', 'phone_number', 'first_name', 'last_name', 'middle_name',
'is_staff', 'is_superuser', 'is_active', 'is_parent', 'is_teacher')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username', 'email', 'phone_number', 'first_name', 'last_name', 'middle_name',
'is_staff', 'is_superuser', 'is_active', 'is_parent', 'is_teacher' ),
}),
)
admin.site.register(CustomUser, CustomUserAdmin),