I have Person
model 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)
Then, I put \n
between obj.first_name
and obj.last_name
as shown below to display first name and last name separately in 2 lines by indentation:
# "admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
list_display = ('person',)
def person(self, obj): # ↓↓ Here
return obj.first_name "\n" obj.last_name
But, first name and last name were displayed in one line without indentation as shown below:
John Smith # One line
So, how can I display first name and last name separately in 2 lines by indentation as shown below:
John # 1st line
Smith # 2nd line
CodePudding user response:
CodePudding user response:
Add person()
inside your model:
# "models.py"
from django.db import models
from django.utils.safestring import mark_safe
class Person(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
def person(self): # Here
return mark_safe(self.first_name '<br>' self.last_name)
Then, put 'person'
to list_display
:
# "admin.py"
from django.contrib import admin
from .models import Person
@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
list_display = ('person',) # Here