I need to display the data from the function which sits inside a model. In the example below, i would like to get the adult of the student which in the getAdult function. What should go inside the ?? to get the data..
models.py:
class Student(models.Model):
name = models.CharField(max_length=255)
age = models.IntegerField(blank=True,
default=0)
birthday = models.DateField(blank=True,
null=True)
school = models.CharField(max_length=255,
blank=True,
default='')
def getAdult(self):
try:
adult = self.relationships.filter()[0].adult
return adult
except IndexError:
return None
admin.py:
from django.contrib import admin
from school.models import Student
class StudentAdmin(admin.ModelAdmin):
list_display=('name',??)
Please pardon me if the question is stupid.
Your
getAdultmethod should return a string. I’m not sure what type of objectadultis, so I’m assuming itstr()will return string representation for it.You can do like this:
For more information refer: list_display in ModelAdmin
Update (if object doesn’t have builtin string representation)