1

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:

enter image description here

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:

enter image description here

So, doesn't __str__() work properly in Person admin?

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
  • Shouldn't you be using the [`display`](https://docs.djangoproject.com/en/4.1/ref/contrib/admin/#the-display-decorator) decorator, if you want to use model methods? – Daniil Fajnberg Jan 14 '23 at 18:37
  • No. What's your point? – Super Kai - Kazuya Ito Jan 14 '23 at 18:43
  • Documentation you linked says: _"There are four types of values that can be used in `list_display`. All but the simplest may use the `display()` decorator, which is used to customize how the field is presented"_ And the example for _"A string representing a model attribute or method (without any required arguments)"_ shows the method being decorated with `display`. – Daniil Fajnberg Jan 14 '23 at 18:51
  • Sorry, I'm not talking about display() decorator. I'm talking if str() doesn't work properly in `Person` admin or not. – Super Kai - Kazuya Ito Jan 14 '23 at 18:55

1 Answers1

0

list_display will lookup field based on model._meta. All class has their own __str__ method, includes your model class which is Person. So list_display will 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__. Hence list_display show like what you got. On the other hand, list_display default value is ("__str__",) equal to what you do above. Here is other approach which might work

admin.py

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    list_display = ('whatever',) # Here

    def whatever(self, obj): # Here
        return obj.first_name + " " + obj.last_name
whoami
  • 141
  • 1
  • 9