一次获取而不要多次获取
DON’T DO
>>> print([e.headline for e in Entry.objects.all()])
>>> print([e.pub_date for e in Entry.objects.all()])
DO
>>> queryset = Entry.objects.all()
>>> print([p.headline for p in queryset]) # 提交查询
>>> print([p.pub_date for p in queryset]) # 重用查询缓存
获取之后的遍历方法
以下4种都可以遍历:
# 方法1
>>> [entry for entry in queryset]
# 方法2
>>> bool(queryset)
# 方法3
>>> entry in queryset
# 方法4
>>> list(queryset)