开发者

How does get_FIELD_display (in django) work?

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 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.


The magic __getattr__ method on classes can be used to implement this kind of functionality. __getattr__ is called for every obj.attribute where attribute does not exist on obj, and the implementation can return whatever it wishes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜