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?
This is all managed via the metaclass – you’ll see in the source that the Model class defines a
__metaclass__attribute, which is set toModelBase. 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_classmethod, which adds any field-specific methods to the class itself – and one of those methods is the_get_FOO_displaymethod. You can see the source for this indjango/db/models/fields/__init__.py.In order to change
_get_FIELD_displaytoget_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.