count专门输出查询结果集的数量,比len()快,count不需要将数据从数据库提取到内存。

count()
count()

Returns an integer representing the number of objects in the database matchingthe QuerySet. The count() method never raises exceptions.

Example:

# Returns the total number of entries in the database.
Entry.objects.count()

# Returns the number of entries whose headline contains 'Lennon'
Entry.objects.filter(headline__contains='Lennon').count()

A count() call performs a SELECT COUNT(*) behind the scenes, so youshould always use count() rather than loading all of the record into Pythonobjects and calling len() on the result (unless you need to load theobjects into memory anyway, in which case len() will be faster).

Note that if you want the number of items in a QuerySet and are alsoretrieving model instances from it (for example, by iterating over it), it’sprobably more efficient to use len(queryset) which won’t cause an extradatabase query like count() would.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值