Changes between Version 2 and Version 22 of Ticket #35742


Ignore:
Timestamp:
Sep 25, 2024, 8:58:29 PM (8 weeks ago)
Author:
antoliny0919
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #35742

    • Property Triage Stage UnreviewedAccepted
    • Property Type New featureCleanup/optimization
    • Property Patch needs improvement set
    • Property Owner set to antoliny0919
    • Property Status newassigned
    • Property Component contrib.admincontrib.auth
    • Property Keywords USERNAME_FIELD added; add_form_template removed
    • Property Summary Apply UserAdmin add_form_template according to User model USERNAME_FIELDDjango'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.
     1Hello!! I made a ticket because I thought that UserAdmin can be improved through USERNAME_FIELD of User model and UserAdmin provided by Django.
    22
    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.
     3This is a way to provide more convenient UserAdmin when django users customize their user models.
     4The add_fieldsetes in UserAdmin currently provided by Django use the static "username".
    75
    86{{{
    9 ...
     7@admin.register(User)
     8class UserAdmin(admin.ModelAdmin):
     9    add_fieldsets = (
     10        (
     11            None,
     12            {
     13                "classes": ("wide",),
     14                "fields": ("username", "usable_password", "password1", "password2"),
     15            },
     16        ),
     17    )
     18}}}
     19Assuming 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
     22class 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
    1034{% block form_top %}
    1135  {% if not is_popup %}
    12     <p>{% translate 'First, enter a username and 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>
    1337  {% else %}
    14     <p>{% translate "Enter a username and password." %}</p>
     38    <p>{% translate "Enter a email and password." %}</p>
    1539  {% endif %}
    1640{% endblock %}
    17 ...
    1841}}}
    19 In the UserAdmin model, when a user uses a custom user model, USERNAME_FIELD allows a field other than username to be used.
     42I think it's better to replace the static "username" with the USERNAME_FIELD dynamic value in this part.
     43{{{
     44# admin.py
     45class CustomUserAdmin(UserAdmin):
     46  add_fieldsets = (
     47    (
     48      None,
     49      {
     50        "classes": ("wide",),
     51        "fields": (User.USERNAME_FIELD, "usable_password", "password1", "password2"),
     52      },
     53    ),
     54  )
    2055
    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}}}
     65Even 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.
    2266
    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.
     67I would also like to know the opinions of Django members and contributors about this improvement.
     68Thank you for reading my improvements :)
Back to Top