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
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
> 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
>> 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.