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 4381216
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:37:17+00:00 2026-05-21T12:37:17+00:00

I’m a novice in Django and Python, and recently came across a number of

  • 0

I’m a novice in Django and Python, and recently came across a number of methods in Django docs, such as Model.get_FOO_display(). Help page says that you can substitute FOO for a name of a field. I’ve been trying to figure out how that is possible in Python and looked into ‘Model’ class souce.
There I came across this:

    def _get_FIELD_display(self, field):
        value = getattr(self, field.attname)
        return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True)

I can’t understand how it’s possible in Python to:
1) to write this

class Person(models.Model):
    gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
p = Person(name='John', gender='M')

p.get_gender_display()

So, how does Python ‘change’ FIELD (or FOO) into gender? Is it some sort of Python ‘wicked’ notation? (Sorry, I can’t ask more clearly) Coud you point to some Python man page?

2) why the source from Model class declares _get_FIELD_display() with the leading underscore, but the Python doc extract from above writes “p.get_gender_display()” without the underscore? Again, could you please give a Python man page for this?

  • 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-05-21T12:37:18+00:00Added an answer on May 21, 2026 at 12:37 pm

    This is all managed via the metaclass – you’ll see in the source that the Model class defines a __metaclass__ attribute, which is set to ModelBase. The metaclass is to the class as a class is to an instance.

    So the metaclass is called when a Django class is defined. The metaclass then executes various code which determines the properties of the model class. So, it iterates through the fields that are declared on the model class, and assigns each one as an instance attribute – this is how Django’s declarative syntax works. It then calls each field’s contribute_to_class method, which adds any field-specific methods to the class itself – and one of those methods is the _get_FOO_display method. You can see the source for this in django/db/models/fields/__init__.py.

    In order to change _get_FIELD_display to get_myfieldname_display, it uses a technique called “currying”, which means that it defines a new function that applies the original method with a defined parameter – in this case, the name of the field.

    If you’re interested in all this, Marty Alchin’s book Pro Django has an excellent explanation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.