简体   繁体   English

Django:如何获取ValuesQuerySet的数量?

[英]Django: How to get count of ValuesQuerySet?

I am trying to get count to work on a ValuesQuerySet . 我试图让count工作在ValuesQuerySet According to Django documentation 根据Django文档

values = Model.objects.values()

will return a ValuesQuerySet which is a subclass of QuerySet 将返回ValuesQuerySet ,它是QuerySet的子类

Returns a ValuesQuerySet — a QuerySet subclass that returns dictionaries when used as an 
iterable, rather than model-instance objects

This would mean that all methods of QuerySet should work on ValuesQuerySet also. 这意味着QuerySet所有方法也应在ValuesQuerySet工作。

However, when I try to do it I get an exception 但是,当我尝试这样做时,会出现异常

values = Model.objects.values()

and then somewhere in my code 然后在我的代码中的某个地方

v_size = size_calc(values)

def size_calc(objects)
    return objects.count()

File "/home/talha/ws/events.py", line 
246, in size_calc
return objects.count()
File "/usr/lib/python2.7/site-packages/django/db/models/query.py", line 336, in count
return self.query.get_count(using=self.db)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 401, in    
get_count
number = obj.get_aggregation(using=using)[None]
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 367, in  
get_aggregation
result = query.get_compiler(using).execute_sql(SINGLE)
File "/usr/lib/python2.7/site-packages/django/db/models/sql/query.py", line 213, in 
get_compiler
return connection.ops.compiler(self.compiler)(self, connection, using)
File "/usr/lib/python2.7/site-packages/django/db/backends/__init__.py", line 582, in 
compiler
return getattr(self._cache, compiler_name)
AttributeError: 'module' object has no attribute 'SQLAggregateCompiler'

count works seamlessly on normal QuerySets .. Could this be an issue with the backend drivers? count在普通QuerySets上无缝运行。.这可能是后端驱动程序出现问题吗?

Update : I cannot use len after evaluating the Queryset as the data is huge and needs to be sliced before evaluation. 更新 :我不能使用len评估后Queryset作为数据是巨大的,需要评估之前切片。

I am using Django with a mongodb backend. 我在mongodb后端使用Django。 Related packages are 相关软件包是

django==1.3.0
django-mongodb-engine==0.4.0
djangotoolbox==0.9.2

The only way I could get past this issue without making changes in the mongodb-engine was to get the count using the normal queryset . 在不更改mongodb-engine下,克服此问题的唯一方法是使用普通的queryset进行计数。 So to get the count I used 所以得到我使用的计数

count = Model.objects.all().count()

and for the data I that wanted, I used 对于我想要的数据,我使用了

values = Model.objects.values('a', 'b')

Why don't you just try len(values) . 为什么不尝试len(values) That works fine 很好

Try this: 尝试这个:

query_list = list(query_set)
len = query_list.indexof(query_list[-1])

This method is nothing similar to len function of list. 此方法与list的len函数相似。 As you might know len uses loop to calculate the length but above method exploits hashing. 您可能知道len使用循环来计算长度,但是上述方法利用了哈希。

Your issue caused because of mongodb aggregation functions. 您的问题是由于mongodb聚合功能引起的。 you can use `Model.objects.item_frequencies('field') or you can read here. 您可以使用 Model.objects.item_frequencies('field') 在此处阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM