Changes between Version 2 and Version 22 of Ticket #35742
- Timestamp:
- Sep 25, 2024, 8:58:29 PM (7 weeks ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #35742
- Property Triage Stage Unreviewed → Accepted
- Property Type New feature → Cleanup/optimization
- Property Patch needs improvement set
- Property Owner set to
- Property Status new → assigned
- Property Component contrib.admin → contrib.auth
- Property Keywords USERNAME_FIELD added; add_form_template removed
- Property Summary Apply UserAdmin add_form_template according to User model USERNAME_FIELD → Django's UserAdmin improvement with dynamic value USERNAME_FIELD
-
Ticket #35742 – Description
v2 v22 1 Hello!! I made a ticket because there was something ambiguous on the Add page of the UserAdmin model.1 Hello!! I made a ticket because I thought that UserAdmin can be improved through USERNAME_FIELD of User model and UserAdmin provided by Django. 2 2 3 The UserAdmin model customizes the ModelAdmin Add page with add_form_template variable. 4 And the template used as the value for that variable is admin/auth/user/add_form.html. 5 6 I felt this part was ambiguous in the add_form template. 3 This is a way to provide more convenient UserAdmin when django users customize their user models. 4 The add_fieldsetes in UserAdmin currently provided by Django use the static "username". 7 5 8 6 {{{ 9 ... 7 @admin.register(User) 8 class UserAdmin(admin.ModelAdmin): 9 add_fieldsets = ( 10 ( 11 None, 12 { 13 "classes": ("wide",), 14 "fields": ("username", "usable_password", "password1", "password2"), 15 }, 16 ), 17 ) 18 }}} 19 Assuming that the user customizes the user model and uses email as an example of USERNAME_FIELD, UserAdmin must be customized by the user as follows. 20 {{{ 21 # admin.py 22 class CustomUserAdmin(UserAdmin): 23 add_fieldsets = ( 24 ( 25 None, 26 { 27 "classes": ("wide",), 28 "fields": ("email", "usable_password", "password1", "password2"), 29 }, 30 ), 31 ) 32 33 # add_form.html 10 34 {% block form_top %} 11 35 {% if not is_popup %} 12 <p>{% translate 'First, enter a usernameand password. Then, you’ll be able to edit more user options.' %}</p>36 <p>{% translate 'First, enter a email and password. Then, you’ll be able to edit more user options.' %}</p> 13 37 {% else %} 14 <p>{% translate "Enter a usernameand password." %}</p>38 <p>{% translate "Enter a email and password." %}</p> 15 39 {% endif %} 16 40 {% endblock %} 17 ...18 41 }}} 19 In the UserAdmin model, when a user uses a custom user model, USERNAME_FIELD allows a field other than username to be used. 42 I think it's better to replace the static "username" with the USERNAME_FIELD dynamic value in this part. 43 {{{ 44 # admin.py 45 class CustomUserAdmin(UserAdmin): 46 add_fieldsets = ( 47 ( 48 None, 49 { 50 "classes": ("wide",), 51 "fields": (User.USERNAME_FIELD, "usable_password", "password1", "password2"), 52 }, 53 ), 54 ) 20 55 21 However, in the above template, <p>{%translate 'First, enter a username and password. Then, you'll be able to edit more user options.'%}</p> This part is target at the default user provided by Django, so it is awkward because "username" is also seen when customizing USERNAME_FIELD using custom users. 56 # add_form.html 57 {% block form_top %} 58 {% if not is_popup %} 59 <p>{% blocktranslate %}First, enter a {{ username }} and password. Then, you’ll be able to edit more user options.{% endblocktranslate %}</p> 60 {% else %} 61 <p>{% blcoktranslate %}Enter a {{ username }} and password.{% endblocktranslate %}</p> 62 {% endif %} 63 {% endblock %} 64 }}} 65 Even if I keep the original code, there is no problem with the function, but if I replace it with a dynamic value as above, I thought it would be more convenient for the django users to customize the User model, so I suggested this part. 22 66 23 Is this part using a static value because it is a ModelAdmin based on the User model provided by Django? 24 25 I added the method to the UserAdmin model and modified add_form.html to show the tag using the USERNAME_FIELD value. 26 27 I respect the great code from the Django maintainer and contributors. But I made a ticket out of my personal opinion that the code would be more curious and in a better way. 28 29 Thank you for reading it. 67 I would also like to know the opinions of Django members and contributors about this improvement. 68 Thank you for reading my improvements :)