Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gooroopy/034ef6e067b82d3a980d114be90856b9 to your computer and use it in GitHub Desktop.
Save gooroopy/034ef6e067b82d3a980d114be90856b9 to your computer and use it in GitHub Desktop.
How to add a field to the Django Admin Add User form using UserCreationForm. Add this to a admin.py and alter to whatever fields you'd like
# How to add a field to the Django Admin Add User form
# using UserCreationForm. Add this to a admin.py and alter
# to whatever fields you'd like
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib import admin
class UserCreationFormExtended(UserCreationForm):
def __init__(self, *args, **kwargs):
super(UserCreationFormExtended, self).__init__(*args, **kwargs)
self.fields['email'] = forms.EmailField(label=_("E-mail"), max_length=75)
UserAdmin.add_form = UserCreationFormExtended
UserAdmin.add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'username', 'password1', 'password2',)
}),
)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment