For example, I define __str__() in Person model as shown below:
# "models.py"
from django.db import models
class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
def __str__(self): # Here
return self.first_name + " " + self.last_name
Then, I define Person admin as shown below:
# "admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
pass
Then, the full name is displayed in the message and list in "Change List" page:
But, when I define __str__() in Person admin as shown below:
# "admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
def __str__(self): # Here
return self.first_name + " " + self.last_name
Or, when I define __str__() then assign it to list_display in Person admin as shown below:
# "admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
list_display = ('__str__',) # Here
def __str__(self): # Here
return self.first_name + " " + self.last_name
The full name is not displayed in the message and list in "Change List" page:
So, doesn't __str__() work properly in Person admin?


list_displaywill lookup field based onmodel._meta. All class has their own__str__method, includes your model class which isPerson. Solist_displaywill use__str__method provided by your model class, even you don't implement it in your model class, it will provide the default implementation of__str__. Hencelist_displayshow like what you got. On the other hand,list_displaydefault value is("__str__",)equal to what you do above. Here is other approach which might workadmin.py