1

Default "Add User" Interface

In this interface I want to add first name and last name field. I know those fields are already present as optional fields in django.contrib.auth.models User model, but they don't show up in UI. Also to make those fields required, is it necessary to override existing User model? I am new to this and have a feeling that doing so would not be a good idea. All help is appreciated!

1 Answers1

0

Subclass the UserCreationForm and use it in your custom user admin:

# forms.py
from django.contrib.auth.forms import UserCreationForm

class CustomUserCreationForm(UserCreationForm):        
    # make fields required if desired
    # first_name = forms.CharField(required=True)

    class Meta(UserCreationForm.Meta):
        fields = ("username", "first_name", "last_name")

# admin.py
from django.contrib.admin.sites import site
from django.contrib.auth.admin import UserAdmin

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2', 
                       'first_name', 'last_name'),
        }),
    )


site.unregister(User)
site.register(User, CustomUserAdmin)
user2390182
  • 72,016
  • 6
  • 67
  • 89