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
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
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
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 }}