#29397: Username validation should be handled at the model level on object
creation.
-----------------------------------------+------------------------
               Reporter:  Andrew Carl    |          Owner:  nobody
                   Type:  Bug            |         Status:  new
              Component:  contrib.admin  |        Version:  1.11
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 I have a rather large django system that conglomerates users from a bunch
 of systems into a single platform. In order to prevent duplicate
 usernames, I am constructing usernames like:
 <username>|<previous_system_id>. I am able to successfully create and
 modify the user object with direct calls to the database. However,
 attempting to modify another username field in the admin site returns a
 validation error. I have attempted to overwrite this form-level validation
 like so:
 {{{
 admin.site.unregister(User)
 username_field = forms.RegexField(label="Username", max_length=150,
     regex=r'^.+$',
     help_text = "Required. 30 characters or fewer. Letters, digits and "
                       "@/./+/-/_/| only.",
     error_messages = {
             'invalid': "This value may contain only letters, numbers and "
                          "@/./+/-/_/| characters."})

 class MyUserCreationForm(UserCreationForm):
     username = username_field

 class MyUserChangeForm(UserChangeForm):
     username = username_field

 @admin.register(User)
 class MyUserAdmin(UserAdmin):
     add_form = MyUserCreationForm
     form = MyUserChangeForm
 }}}

 This, however will return the error:
  This value may contain only letters, numbers and @/./+/-/_/| characters.

 This leads me to believe that my user admin form is correctly being
 overridden, as my custom error message is displayed. I am currently able
 to modify and save my user model by setting the 'username' field to be
 read only.

 The fact that I am able to save on the out of the box user model with an
 "invalid" character, but then have the stock admin site break I would
 consider unexpected behavior. This would be acceptable if the form
 validator was able to be overridden or if I was thrown an exception on
 object creation.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29397>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.db964d204aa789175ddd2399b0151512%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to