﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35742	"Avoid refering to ""username"" in admin templates as USERNAME_FIELD can be updated"	antoliny0919	antoliny0919	"Hello!! I made a ticket because I thought that UserAdmin can be improved through USERNAME_FIELD of User model and UserAdmin provided by Django.

This is a way to provide more convenient UserAdmin when django users customize their user models.
The add_fieldsetes in UserAdmin currently provided by Django use the static ""username"".

{{{
@admin.register(User)
class UserAdmin(admin.ModelAdmin):
    add_fieldsets = (
        (
            None,
            {
                ""classes"": (""wide"",),
                ""fields"": (""username"", ""usable_password"", ""password1"", ""password2""),
            },
        ),
    )
}}}
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.
{{{
# admin.py
class CustomUserAdmin(UserAdmin):
    add_fieldsets = (
        (
            None,
            {
                ""classes"": (""wide"",),
                ""fields"": (""email"", ""usable_password"", ""password1"", ""password2""),
            },
        ),
    )

# add_form.html
{% block form_top %}
  {% if not is_popup %}
    <p>{% translate 'First, enter a email and password. Then, you’ll be able to edit more user options.' %}</p>
  {% else %}
    <p>{% translate ""Enter a email and password."" %}</p>
  {% endif %}
{% endblock %}
}}}
I think it's better to replace the static ""username"" with the USERNAME_FIELD dynamic value in this part.
{{{
# admin.py
class CustomUserAdmin(UserAdmin):
  add_fieldsets = (
    (
      None,
      {
        ""classes"": (""wide"",),
        ""fields"": (User.USERNAME_FIELD, ""usable_password"", ""password1"", ""password2""),
      },
    ),
  )

# add_form.html
{% block form_top %}
  {% if not is_popup %}
    <p>{% blocktranslate %}First, enter a {{ username }} and password. Then, you’ll be able to edit more user options.{% endblocktranslate %}</p>
  {% else %}
    <p>{% blcoktranslate %}Enter a {{ username }} and password.{% endblocktranslate %}</p>
  {% endif %}
{% endblock %}
}}}
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.

I would also like to know the opinions of Django members and contributors about this improvement.
Thank you for reading my improvements :)"	Cleanup/optimization	closed	contrib.auth	5.1	Normal	fixed	UserAdmin, USERNAME_FIELD		Ready for checkin	1	0	0	0	0	0
