2👍
✅
Your getAdult
method should return a string. I’m not sure what type of object adult
is, so I’m assuming it str()
will return string representation for it.
You can do like this:
class Student(models.Model):
#your stuff
def getAdult(self):
try:
adult = self.relationships.filter()[0].adult
return str(adult)
except Exception:
return ''
getAdult.shot_description = 'Adult'
class StudentAdmin(admin.ModelAdmin):
list_display=('name', 'getAdult')
For more information refer: list_display in ModelAdmin
Update (if object doesn’t have builtin string representation)
#example Adult model
class Adult(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
age = models.IntegerField()
class Student(models.Model):
#other stuff
def getAdult(self):
try:
adult = self.relationships.filter()[0].adult
return '%s %s' % (adult.first_name, adult.last_name)
except Exception:
return ''
getAdult.shot_description = 'Adult'
class StudentAdmin(admin.ModelAdmin):
list_display=('name', 'getAdult')
Source:stackexchange.com