I have the ModelForm below to let the user edit some of their basic
User fields like username, first/last name and email. During testing,
I discovered that I could edit and save a username that included non-
alpha characters. When I log into the admin system, the username is
set to the new value. I can log back in with the new username as
well. It seems to work OK, but I wonder if it could cause problems
elsewhere. How do I get it to do the same username validation as at
creation?
class UserForm( ModelForm ):
class Meta:
model = User
exclude = ( 'password', 'is_staff', 'is_superuser',
'last_login',
'is_active', 'date_joined' )
def clean_username( self ):
old_username = self.instance.username
new_username = self.cleaned_data.get( 'username', '' )
if old_username != new_username:
try:
User.objects.get( username=new_username )
except User.DoesNotExist:
return new_username
raise forms.ValidationError( 'A user with this username
already
exists. Please user another.' )
return new_username
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---