count() performs a SELECT COUNT(*)
behind the scenes, so you should
always use count() rather than loading
all of the record into Python objects
and calling len() on the result
(unless you need to load the objects
into memory anyway, in which case
len() will be faster).
从Django 1.2开始,您可以在QuerySet上使用
.exists()
,但在以前的版本中,您可能会享受到this ticket中描述的非常有效的技巧。更新:
正如最近的回答中提到的,自从Django 1.2以来,您可以使用
exists()
方法来代替(link)。原始答案:
不要对结果使用len(),应该使用
People.objects.filter(Name='Fred').count()
。根据django的文件来源:Django docs
QuerySet API中的exists()方法可用since Django 1.2。
相关问题 更多 >
编程相关推荐