views:

63

answers:

1

I have a models class class A(models.Model): user = models.ForeignKey(User)

Now if I do a a = A.objects.get(pk = something_existing); print a.__dict__, user is not in __dict__. a.user however doesnot give an attribute error.

So when is the actual user being calculated? I looked in django.db.models.base.Model and django.db.models.base.ModelBase classes and they do not override __getattr__, (which I guessed might be being done), so where is this calculated/populated.

+3  A: 

Django relation fields are implemented as descriptors.

Ignacio Vazquez-Abrams
Ok, but I dont see a `__get__` and family in `models.Model` or metaclass, so how is this a descriptor?
uswaretech
The model is not a descriptor, the fields are.
Ignacio Vazquez-Abrams
To be clear, related fields (ForeignKey, ManyToMany, etc) are implemented as descriptors (look in db/models/fields/related.py). Most fields (CharField, IntegerField) do not use descriptors.
Carl Meyer
Edited the answer to be more accurate.
Carl Meyer