A ValuesQuerySet is useful when you know you're only going to need
values from a small number of the available fields and you won't need
the functionality of a model instance object.
len(). A QuerySet is evaluated when you call len() on it. This, as you might expect, returns the length of the result list.
Note: Don't use len() on QuerySets if all you want to do is determine the number of records in the set. It's much more efficient
to handle a count at the database level, using SQL's SELECT COUNT(*),
and Django provides a count() method for precisely this reason. See
count() below.
Riateche回答了这个问题,但是如果不想直接在QuerySet上使用len()或.count(),可以返回一个values_list并在上面使用len()。
Django文件说:
https://docs.djangoproject.com/en/dev/ref/models/querysets/
我怀疑这是再快,但你可以随时看到它的时间。如果您只想返回表中可用字段总数的子集,那么它可能很有用。
Source
因此,如果调用
len(userdesigns)
,而不是userdesigns.count()
,django将在单个查询中从表中请求所有相关数据。之后,您可以访问userdesigns
变量的所有项,不会进行其他查询。相关问题 更多 >
编程相关推荐