best practice to change allowed characters in a username

194 views
Skip to first unread message

Benedict Verheyen

unread,
Oct 29, 2008, 11:39:13 AM10/29/08
to django...@googlegroups.com
Hi,


i want to know what the best practice is for following problem.
I use the built in user authentication framework. I've already made the login
authenticate against our AD. Works fine (see http://www.djangosnippets.org/snippets/901/)

In our company, usernames are specified with a period between the first
and last name. When i want to edit or add a user with the admin view,
i get a warning.
To change this behaviour, i edited UserCreationForm and UserChangeForm from django\contrib\auth\forms.py.
This doesn't feel right as changes will be lost when i update django (i track svn so it happens :))
so how would i go about to change this in a decent way?

I tried to override the 2 forms in my models.py file

class MyUserCreationForm(UserCreationForm):
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[-\w.@+]+$',
help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits, underscores,
period, @ and +)."),
error_message = _("This value must contain only letters, numbers and underscores."))
class Meta:
model = User
fields = ("username",)

class MyUserChangeForm(UserChangeForm):
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^[-\w.@+]+$',
help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits, underscores,
period, @ and +)."),
error_message = _("This value must contain only letters, numbers and underscores."))
class Meta:
model = User

Then in my admin.py file:
class MyUserAdmin(admin.ModelAdmin):
form = MyUserChangeForm
add_form = MyUserCreationForm
admin.site.register(User, MyUserAdmin)

But then i get the error message "The model User is already registered"
I had the same error when i wanted to change the permissions
on the default User.

Thanks,
Benedict

Jarek Zgoda

unread,
Oct 29, 2008, 11:48:55 AM10/29/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-10-29, o godz. 16:39, przez Benedict
Verheyen:


You have to unregister the admin class for User model before
registering your own.

--
We read Knuth so you don't have to. - Tim Peters

Jarek Zgoda, R&D, Redefine
jarek...@redefine.pl

Benedict Verheyen

unread,
Oct 29, 2008, 11:57:32 AM10/29/08
to django...@googlegroups.com
Jarek Zgoda wrote:

> You have to unregister the admin class for User model before
> registering your own.

Thanks, that seems to solve the register error.
However, the other question remains, is the way i'm going about to
change the allowed characters ok?

Thanks,
Benedict

Jarek Zgoda

unread,
Oct 29, 2008, 3:19:05 PM10/29/08
to django...@googlegroups.com
Wiadomość napisana w dniu 2008-10-29, o godz. 16:57, przez Benedict
Verheyen:

>> You have to unregister the admin class for User model before
>> registering your own.
>
> Thanks, that seems to solve the register error.
> However, the other question remains, is the way i'm going about to
> change the allowed characters ok?


It's OK if it works. I think the only problem is form validation, so
registering your own form seems to be the way. Anyway, it should not
break anything.

Reply all
Reply to author
Forward
0 new messages