Lazy loading relationships in Django (and other MVCs/ORMs)

拥有回忆 提交于 2019-12-10 13:56:20

问题


Interested in knowing how lazy loading is achieved in frameworks like Django. When is the decision made to perform the join? And is there a way to force eager loading in Django? Are there times when you would need to force Django to eager load?


回答1:


The general answer is that Django makes the decision to perform the query when you actually ask for some records. Most commonly this means iterating over the queryset (for record in queryset:) or using the list() built-in function to convert the queryset to a list.

See When QuerySets are evaluated for more specifics from the official docs.

It accomplishes this by defining a class, called QuerySet in django/db/models/query.py, where the special methods like __repr__, __getitem__ and __iter__ are coded to do the right thing.

If you need to force eager loading just run the built-in Python list function on the queryset, like:

qs = SomeModel.objects.all()
ql = list(qs)

This call to list() will perform the DB query and load all of the objects into memory. It should be pretty rare that you need to do this, but one case is when you need to use the query results in more than one place in your templates. Converting to list and passing the list in your template context will perform the query only once instead of once for every place in your template you iterate.



来源:https://stackoverflow.com/questions/1085378/lazy-loading-relationships-in-django-and-other-mvcs-orms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!