Making email required

102 views
Skip to first unread message

Larry Martell

unread,
Jan 31, 2017, 3:58:27 PM1/31/17
to django...@googlegroups.com
I want to make the email field required in the user admin add and
change pages. Following some posts I read on stackoverflow I did this:

class MyUserCreationForm(UserCreationForm):
def __init__(self, *args, **kwargs):
super(MyUserCreationForm, self).__init__(*args, **kwargs)
# make user email field required
self.fields['email'].required = True

class UserAdmin(BaseUserAdmin):
form = MyUserCreationForm
add_form = MyUserCreationForm
add_fieldsets = ((None, {'fields': ('username', 'email',
'password1', 'password2'), 'classes': ('wide',)}),)

admin.site.unregister(User)
admin.site.register(User, UserAdmin)

This works fine in add user, but in change user I get the user's
encrypted password shown in the password field, instead of what you
normally see:

algorithm: pbkdf2_sha256 iterations: 24000 salt: ****** hash:
**************************************
Raw passwords are not stored, so there is no way to see this user's
password, but you can change the password using this form.

And when I try to save from the change screen it says "Please correct
the errors below." even though there are no errors shown.

How can I fix these issues in the change form?

jorr...@gmail.com

unread,
Feb 1, 2017, 5:32:35 AM2/1/17
to Django users
You can define a single form to use for creating and updating users like this:

class CustomUserChangeForm(UserChangeForm):
 
class Meta:
 

 model
= User
 

 fields
= ('email', 'password', 'first_name', 'last_name', 'photo', 'is_active', 'is_staff', 'user_permissions')
 

 
 
 

 
 
 

class CustomUserAdmin(UserAdmin):
 

 
# Override standard fields
 

 form
= CustomUserChangeForm
 

 fieldsets
= None
 

 add_fieldsets
= (None, {
 

 
'classes': ('wide',),
 

 
'fields': ('email', 'password1', 'password2', 'first_name', 'last_name', 'is_staff'),
 
 
}),

Setting fieldsets = None on the CustomUserAdmin makes it inherit the fields from your form to use when updating a user, and setting add_fieldsets to the fields you want lets you specify the fields you want to use when creating a user.

The problem you are having is that Django only recognizes a field named 'password' as a special case, and not 'password1' and 'password2', so the latter two are only to be used when you actually do need to input the unhashed password,

Larry Martell

unread,
Feb 1, 2017, 8:21:48 AM2/1/17
to django...@googlegroups.com
Thanks. I got this working. I did something like this:

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User

class EmailRequiredMixin(object):
def __init__(self, *args, **kwargs):
super(EmailRequiredMixin, self).__init__(*args, **kwargs)
# make user email field required
self.fields['email'].required = True


class MyUserCreationForm(EmailRequiredMixin, UserCreationForm):
pass


class MyUserChangeForm(EmailRequiredMixin, UserChangeForm):
pass


class EmailRequiredUserAdmin(UserAdmin):
form = MyUserChangeForm
add_form = MyUserCreationForm
add_fieldsets = ((None, {'fields': ('username', 'email',
'password1', 'password2'),
'classes': ('wide',)}),)

admin.site.unregister(User)
admin.site.register(User, EmailRequiredUserAdmin)
Reply all
Reply to author
Forward
0 new messages