How does Django's ORM manage to fetch Foreign objects when they are accessed

Viewed 7749

Been trying to figure this out for a couple of hours now and have gotten nowhere.

class other(models.Model):
    user = models.ForeignKey(User)


others = other.objects.all()
o = others[0]

At this point the ORM has not asked for the o.user object, but if I do ANYTHING that touches that object, it loads it from the database.

type(o.user)

will cause a load from the database.

What I want to understand is HOW they do this magic. What is the pythonic pixie dust that causes it to happen. Yes, I have looked at the source, I'm stumped.

3 Answers
Related