切片时查询集被评估?

Queryset is evaluated when sliced?

在 django queryset doc(https://docs.djangoproject.com/en/1.10/ref/models/querysets/) 中,它说 "Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset." 然后,它说 "You can evaluate a QuerySet in the following ways: Iteration / Slicing / . . ." .

所以,我的问题是 "whether Queryset is evaluated when sliced" .

完整段落解释了切片何时会评估查询集,何时不会(强调我的):

  • Slicing. As explained in Limiting QuerySets, a QuerySet can be sliced, using Python’s array-slicing syntax. Slicing an unevaluated QuerySet usually returns another unevaluated QuerySet, but Django will execute the database query if you use the “step” parameter of slice syntax, and will return a list. Slicing a QuerySet that has been evaluated also returns a list.

Django 只会在您使用 step 参数时评估查询集,例如 queryset[::2],因为这不能转换为 SQL 查询。在其他情况下,切片未评估的查询集将 return 另一个未评估的查询集,Django 将添加 LIMIT and/or OFFSET 到查询中。