Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Display "Model Object" in the admin page instead of Object title

As displayed in the image it displays "Lecture Object" instead of the Lecture's title. As I've understood it, unicode should take care of this, but it doesn't seem to here.

Here is my unicode method:

def __unicode__(self):
    return self.title
like image 745
tryingtolearn Avatar asked Nov 18 '15 15:11

tryingtolearn


People also ask

What is __ Str__ in Django?

The __str__ method just tells Django what to print when it needs to print out an instance of the any model. It is also what lets your admin panel, go from this. Note: how objects are just plainly numbered.

How do I change my Django admin panel name?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.

Can we change Django admin theme?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.


1 Answers

To display a custom string as your Model's object representation, you should:

In Python 2.x

def __unicode__(self):
    return self.some_attr  # What you want to show

In Python 3.x

def __str__(self):
    return self.some_attr  # What you want to show
like image 151
Gocht Avatar answered Sep 22 '22 13:09

Gocht