擅长:python、mysql、java
<p>django提供了<a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.exists" rel="nofollow">^{<cd1>}</a>的一个非常好的概述</p>
<p>使用第一个示例,它将根据<a href="https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.exists" rel="nofollow">documentation</a>执行两次查询:</p>
<blockquote>
<p>if some_queryset has not yet been evaluated, but you
know that it will be at some point, then using some_queryset.exists()
will do more overall work (one query for the existence check plus an
extra one to later retrieve the results) than simply using
bool(some_queryset), which retrieves the results and then checks if
any were returned.</p>
</blockquote>
<p>因此,如果你打算使用这个对象,在检查是否存在之后,文档建议你只使用它,并强制使用一次评估</p>
<pre><code>if my_object:
pass
</code></pre>