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?