Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8887003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:41:56+00:00 2026-06-14T21:41:56+00:00

Thie is the source code in Django, when change the permission for a user

  • 0

Thie is the source code in Django, when change the permission for a user named “xxx///\”, it will have an error, but in fact i don’t want to block this format username, so i want to overwrite the UserChangeForm class. Could you please tell me how to do this? Thanks!

class UserChangeForm(forms.ModelForm):
    username = forms.RegexField(
        label=_("Username"), max_length=30, regex=r"^[\w.@+-]+$",
        help_text = _("Required. 30 characters or fewer. Letters, digits and "
                      "@/./+/-/_ only."),
        error_messages = {
            'invalid': _("This value may contain only letters, numbers and "
                         "@/./+/-/_ characters.")})
    password = ReadOnlyPasswordHashField(label=_("Password"),
        help_text=_("Raw passwords are not stored, so there is no way to see "
                    "this user's password, but you can change the password "
                    "using <a href=\"password/\">this form</a>."))

    def clean_password(self):
        return self.initial["password"]

    class Meta:
        model = User

    def __init__(self, *args, **kwargs):
        super(UserChangeForm, self).__init__(*args, **kwargs)
        f = self.fields.get('user_permissions', None)
        if f is not None:
            f.queryset = f.queryset.select_related('content_type')

I finally find the method,add this in the Admin.py

from django.contrib.auth.admin import UserAdmin
# overwrite the UserChangeForm
class UserChangeForm(forms.ModelForm):
    username = forms.RegexField(
        label=_("Username"), max_length=30, regex=r"^[a-zA-Z0-9@\.+-_\/\\]+$",
        help_text = _("Required. 30 characters or fewer. Letters, digits and "
                      "@/./+/-/_ only."),
        error_messages = {
            'invalid': _("This value may contain only letters, numbers and "
                         ".")})
UserAdmin.form = UserChangeForm

Thanks all the same

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:41:57+00:00Added an answer on June 14, 2026 at 9:41 pm

    Your regex isn’t doing what you want. Find some tutorials on regex (google ‘Regex Tutorial’). Then I’d suggest getting some software to test regex (Regex Coach on windows is great).

    I think the expression ^[a-zA-Z0-9@\.+-_\/\\]+$. Should do what you’re looking for. You’ll need to change your help and error text too – you’re actually accepting additional characters to the ones you say you’re accepting. Lastly, add a max_length field to match your existing help text.

    class CustomChangeForm(forms.ModelForm):
        username = forms.RegexField(
            label=_("Username"), max_length=30, regex=r"^[a-zA-Z0-9@\.+-_\/\\]+$",
            help_text = _("Required. 30 characters or fewer. Letters, digits and "
                          "@.+-_/\ only."),
            error_messages = {
                'invalid': _("This value may contain only letters, numbers and "
                             "@.+-_/\ characters.")}),
            max_length=30)
            ....
    

    EDIT

    To override the UserChangeForm rewrite the class statement so your form inherits from UserChangeForm instead of ModelForm –

    from django.contrib.auth.forms import UserChangeForm
    
    class CustomChangeForm(UserChangeForm):
        username = forms.RexField(
            ...
    

    Have a look at the docs for more information on inheritance with Django forms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

for the last hour I have been trying to figure this out myself but
Suppose I have a UITableView with two sections. If the data source for that
For thie HTML : <div id=a> <div id=b> </div> </div> when I click b
I got a CWnd like thie CWnd * pWnd = pDC->GetWindow(); Is there away
I am working with some test code to create Visio documents. I am using
I have a problem on my mac when trying in R to produce png
The class contain this parameter like thie public class Global { public static String
I have problem with getting username form windows system. I tried using getlogin function
I am trying to get the following code to work so I can call
I'm having bit complicated object model that forms a triangle. There is User entity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.