0

I need to display information in Admin from def str like a self.name + self.last_name, but at the same time I need to show only name value in templates. How I can do it?

Now I see the same self.name + self.last_name for both

Oleg
  • 777
  • 1
  • 6
  • 10

2 Answers2

1

You can define custom method in your model for admin page and use __str__ method for other things

def admin_name(self):
    return '{} {}'.format(self.name, self.last_name)

admin_name.short_description = 'Full name'

and just append this in list_display of your admin model representation in admin.py

Ivan Semochkin
  • 8,649
  • 3
  • 43
  • 75
0

It's not possible to make the __str__ method behave differently in different views.

If you don't want to use the __str__ method in the templates, use the individual attributes instead:

{{ user.name }}
Alasdair
  • 298,606
  • 55
  • 578
  • 516