The problem:
I would like to use django admin site User model to register new users. The default option offers you to register new username with password and password confirmation, which are required fields.
My idea is to make few changes. I would like to keep username field and add email-field, however delete password and password confirmation. The principal idea is, I would register a user and he would receive an email with generated password. He would then be able to login with generated password and change the password.
I am facing huge problems with templates, errors and most of all overriding the classes.
I have spent many many hours checking django documentation, goolge and the best I could do so far is:
admin.py
from django.contrib import admin
from django.contrib.auth import admin as upstream
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import Group, User
from django.utils.translation import ugettext, ugettext_lazy as _
class UserAdmin(upstream.UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'password','email')}),
(_('Personal info'), {'fields': ('first_name', 'last_name')}),
(_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
'groups', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('username','email')}
),
)
form = UserChangeForm
add_form = UserCreationForm
try:
admin.site.unregister(User)
except NotRegistered:
pass
admin.site.register(User, UserAdmin)
This is a code I have found on stackoverflow. Within add_fieldsets I removed password1, and password2. If I go to admin page, everything looks great, however I can not register a user.
I think it is because of UserCreationForm
, which insists of creating an password, but perhaps I could be wrong. Can anyone please let me know how to override UsercreationForm
, so passwords wouldn't be mandatory anymore and errors from UserCreationForm
would not be seen on the site.