email as username, issue with admin page

6 views
Skip to first unread message

selcukcihan

unread,
Jul 16, 2009, 5:16:31 AM7/16/09
to Django users
Hi, i am using email as username. Both username and email fields in
User model always have the same value which is an email.

From previous posts and code snippets, i got logins authentication and
other stuff working. However, i just can't seem to get the admin page
to create/modify user's whose username is an email. UserChangeForm &
UserCreationForm tell me "This value must contain only letters,
numbers and underscores.".

To overcome this issue, i wrote the following code

class OurUserChangeForm(UserChangeForm):
username = forms.EmailField

class OurUserCreationForm(UserCreationForm):
username = forms.EmailField

class OurUserAdmin(UserAdmin):
form = OurUserChangeForm
add_form = OurUserCreationForm

and i register with
admin.site.unregister(User)
admin.site.register(User, OurUserAdmin)

but it is not working. Any help/pointers appreciated

Phil

unread,
Jul 16, 2009, 7:23:03 AM7/16/09
to Django users
Hi,

as a simple idea you may overcome that by prompting user to enter
email and then generate username for a User model, if you mean it,
yourself. Of course you can leave option to enter username, but leave
email as required or allow to enter any of those in the same field.
Kind of 'slugify' will help.

apologise if that isn't what you're looking for.

Cheers

selcukcihan

unread,
Jul 18, 2009, 8:01:18 AM7/18/09
to Django users
Hi, thanks for the tip. I decided to just replicate
auth.forms.UserCreationForm and auth.forms.UserChangeForm as
OurUserCreationForm and OurUserChangeForm
Ofcourse with the difference of having username as a forms.EmailField

In admin.py of my application i did

admin.site.unregister(User)
UserAdmin.form = OurUserChangeForm
UserAdmin.add_form = OurUserCreationForm
admin.site.register(User, UserAdmin)

now it is working as needed

selcukcihan

unread,
Jul 18, 2009, 8:04:53 AM7/18/09
to Django users
Sorry i forgot to post another change i did in OurUserCreationForm
in the method "save"
i have
user = super(OurUserCreationForm, self).save(commit=False)
instead of
user = super(UserCreationForm, self).save(commit=False)

Copy&Pasting the code is not a good solution at all, but it works. If
only i could derive from UserCreationForm and UserChangeForm to
reflect the changes, but i could not get it working with inheritance
and i am a relatively novice python programmer
Reply all
Reply to author
Forward
0 new messages