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

后端 未结 3 1894
北海茫月
北海茫月 2021-01-31 04:47

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.         


        
3条回答
  •  旧时难觅i
    2021-01-31 05:12

    This will not explain how exactly Django goes about it, but what you are seeing is Lazy Loading in action. Lazy Loading is a well known design pattern to defer the initialization of objects right up until the point they are needed. In your case until either of o = others[0] or type(o.user) is executed. This Wikipedia article may give you some insights into the process.

提交回复
热议问题