How to change the user display name to their first name in Django
For any such changes, you should refer to the template that's rendering it. Here, it is the Django Admin's base.html
template.
As you can see in this line in the file, it searches for both short_name
and user_name
in that order and displays the first one available.
{% block welcome-msg %}
{% translate 'Welcome,' %}
<strong>
{% firstof user.get_short_name user.get_username %}
</strong>.
{% endblock %}
And get_short_name
returns the first name of the user. So, your user does not have their first name defined and hence it's showing up their username.
NOTE : Please check your Django version's documentation since this has been implemented after version 1.5 and above, and is valid only for greater versions.
I highly recommend to extend the auth user model and thus you can do a lot of customizations sooner or later.
You can use lambda
function to alter the __str__()
method of auth user as
from django.contrib.auth.models import User
User.__str__ = lambda user_instance: user_instance.first_name
Note: This snippet should get executed on the Django server initialization
If you are trying to change the username which is shown at the top-right-corner (as mentioned by @amit sigh ), set the lambda function for get_short_name
as,
from django.contrib.auth.models import User
User.get_short_name = lambda user_instance: f"Prefix : {user_instance.first_name} : Suffix"