[Fixed]-How to do different data representation from model def __str__ in Admin and Templates

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

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

Leave a comment