Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:04:44+00:00 2026-06-09T04:04:44+00:00

I need to display the data from the function which sits inside a model.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:04:45+00:00Added an answer on June 9, 2026 at 4:04 am

    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')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a situation where we need to display data from a database in
I need to display a real time data from MS SQL 2005. I saw
I need to display short description like label and near it data associated with
I want to display some data which comes from my db using jQuery.ajax() (i.e.
In my WPF app I have a data import function which imports data from
How can I bind data from a function inside a KnockoutJS foreach ? e.g.
I get the data from DB and display it in a div... what I
I need to be able to display data that I have in 15 minute
I need to display a large amount of data (around 50-60K rows). I was
I have 1000 rows of data but need to only display 100 rows at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.