Issue
On Django Admin, the default 'create user' form has 3 fields: username, password and confirm password.
I need to customize the create user form. I want to add the firstname
and lastname
fields, and autofill the username field with firstname.lastname
.
How can I do this?
Solution
Something like this should work:
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class UserCreateForm(UserCreationForm):
class Meta:
model = User
fields = ('username', 'first_name' , 'last_name', )
class UserAdmin(UserAdmin):
add_form = UserCreateForm
prepopulated_fields = {'username': ('first_name' , 'last_name', )}
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('first_name', 'last_name', 'username', 'password1', 'password2', ),
}),
)
# Re-register UserAdmin
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Answered By - mishbah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.