3

I am using the Django inbuilt user authentication system, from from django.contrib.auth.models import User. And I have realised that in the admin page it always displays the username of the user. Is it possible to change the def __str__ (self): method of that function to display a customized one, something like this.

def __str__ (self):
    return f"{self.first_name}"
JPG
  • 82,442
  • 19
  • 127
  • 206
Najaaz
  • 380
  • 5
  • 16
  • Please reformat your question and add maybe a screenshot of what part of the admin you want to change? Because it shows first name in my admin landing page and not username. – Amit Singh Jan 03 '21 at 04:37
  • if you want to show that name anywhere except django admin panel. use `user.firat_name`. but for more coding as django doc says you should override a user model over djangos user model and then override the __str__ method of that class. – mh-firouzjah Jan 03 '21 at 04:46

2 Answers2

4

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.

Tobit
  • 406
  • 7
  • 19
Amit Singh
  • 2,875
  • 14
  • 30
3

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"
JPG
  • 82,442
  • 19
  • 127
  • 206
  • I don't think extending the User model would help in this particular usecase. – Amit Singh Jan 03 '21 at 04:52
  • Can you explain why?? OP wants to customize the `__str__()` of the ***built-in `User` model*** (isn't it?) extending the model ***definitely*** give you more control over your models @AmitSingh – JPG Jan 03 '21 at 04:57
  • OP wants to change the name shown at the top right corner of the admin console page. And extending the User model won't help with that. – Amit Singh Jan 03 '21 at 04:58
  • that top-r-c thing didn't come up in my mind, that's also a possibility, but he OP bit confusing though. Also, I strongly disagree with your argument *"extending the User model won't help with that"*, because, you will be able to *override* the `get_short_name` method in your custom model and Django will use the custom method instead of built-in @AmitSingh – JPG Jan 03 '21 at 05:11
  • You don't need to override the `get_short_name` because it returns the first name, in it's implementation. https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.get_short_name – Amit Singh Jan 03 '21 at 05:13
  • What if I want to display ***WELCOME, Mr/Mrs {first_name}***? – JPG Jan 03 '21 at 05:15
  • That would obviously mean changing the template too, if you want to add that customization but the OP has asked only about returning the first name. Also, I would not suggest overriding a function that has been documented to return first name to suddenly start returning "Welcome, Mr/Mrs {first name}" that would be bad programming. But yes, it would require changes in the user model too. – Amit Singh Jan 03 '21 at 05:20
  • *"OP has asked only about returning the first name"*: I would rather read it as *"customizing the representation"* not just *returning the firstname*. I don't see any *bad programming* when I override things, because I need the changes in my project – JPG Jan 03 '21 at 05:23
  • You are trying to override the `User.get_short_name` in your answer which is bad programming. Extending it to another model and then overriding is something I would totally agree upon, but the correct suggested solution is bad programming. – Amit Singh Jan 03 '21 at 05:27
  • Customizing the representation would definitely need help with extension of the model, true. – Amit Singh Jan 03 '21 at 05:28
  • Well what I wanted was to change the display name of the user in the admin panel and it helped by doing `User.__str__ = lambda user_instance: user_instance.first_name`. Thanks a lot for all your help and efforts! – Najaaz Jan 03 '21 at 13:58