[英]How to count a filtered result in Django?
I have managed to create a QuerySet of what I want so far, but I am unable to include the .count ()
statement in it.到目前为止,我已经设法创建了一个我想要的 QuerySet,但我无法在其中包含
.count ()
语句。 I do know that I can use len (students)
, but I would like to stick to the Django ORM.我知道我可以使用
len (students)
,但我想坚持使用 Django ORM。
student_count = Student.objects.filter(last_online__range=[last week, this week])
How do I do that?我怎么做?
you can use aggregate()
to count, like this:您可以使用
aggregate()
进行计数,如下所示:
student_count = Student.objects.filter(last_online__range=[last week, this week]).aggregate(Count('MODEL_FILE'))
The function of Django's aggregate() method is to perform statistical calculations on a set of values (such as a field of the queryset) and return the statistical calculation results in a dictionary (Dict) format. Django的aggregate()方法的function是对一组值(比如queryset的某个字段)进行统计计算,并将统计计算结果以字典(Dict)格式返回。
You can use count()
您可以使用
count()
Student.objects.filter(last_online__range=[last week, this week]).count()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.