Django QuerySets ORM什么时候执行计算求值(When QuerySets are evaluated)

200 阅读1分钟

When QuerySets are evaluated

You can concatenate as many filters as you like to a QuerySet, and you will not hit the database until the QuerySet is evaluated. QuerySets are only evaluated in the following cases:

  1. The first time you iterate over them
  2. When you slice them, for instance, Post.objects.all()[:3]
  3. When you pickle or cache them
  4. When you call repr() or len() on them
  5. When you explicitly call list() on them
  6. When you test them in a statement, such as bool(), or , and, or if

from book Django by example 2nd edition

Django 官方文档的描述: docs.djangoproject.com/en/2.1/topi…

changchen.me/blog/201705…