简体   繁体   English

如何计算我的 QuerySet 的数量。 Django

[英]How to Count quantity of my QuerySet. Django

Well, i have the following function:好吧,我有以下 function:

employees_birthday = Employees.objects.filter(EmployeeDataNasc__gte=first_day, EmployeeDataNasc__lte=last_day)

It serves to return employees born between the dates.它用于返回在日期之间出生的员工。 She returns:她回来了:

<QuerySet [<Employees: Alberto Santos>, <Employees: Josney Arman>]>

But, I would just like to display the number of employees in the QuerySet, ie make a Count function.但是,我只想显示 QuerySet 中的员工数量,即计算 function。

Can someone help me with the syntax?有人可以帮我语法吗?

If you are going to display the Employees anyway, then it is better to use len(…) .如果无论如何都要显示Employees ,那么最好使用len(…) This will fetch the Employees in memory, and return the number of Employees :这将获取 memory 中的Employees ,并返回Employees的数量:

employees_birthday = Employees.objects.filter(
    EmployeeDataNasc__range=(first_day, last_day)
)
number_of_birthdays = len(employee_birthdays)

if you however plan to only render the number of Employees , then you use .count() [Django-doc] , since this will prevent fetching the items in memory.但是,如果您打算呈现Employees数量,那么您可以使用.count() [Django-doc] ,因为这将阻止获取 memory 中的项目。

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

相关问题 如何保存从QuerySet返回的随机项目。 Django,Python - How to save a random item returned from QuerySet. Django, Python 应用于查询集的列表类的count()方法。 奇怪的行为。 Django的 - count() method of the list class applied to queryset. Strange behaviour. Django Django查询集。 将输出转换为列表 - Django queryset. Convert output to list Django - 从 Elasticsearch 获取数据作为 QuerySet。 - Django - fetch data from Elasticsearch as QuerySet. Django:如何通过过滤器查询集从 ForeignKey model 返回字段。 因为它不会抛出 JSON 可序列化 - Django: How to return a field from a ForeignKey model through filter queryset. As it throws not JSON Serializable IndexView 缺少 QuerySet。 定义 IndexView.model - 教程 4 django - IndexView is missing a QuerySet. Define IndexView.model - tutorial 4 django Django查询集。 将所有行保存到变量并删除表中的行 - Django queryset. Save all rows to a variable and delete rows in table Django 从查询集中获取查询。 返回无效参数 - Django get query from queryset. Returning invalid params 如何通过查询集获取最新记录。 喜欢分组 - How can I get latest record by queryset. like group by django 1.8:XView缺少QuerySet。 定义XView.model,XView.queryset, - django 1.8:XView is missing a QuerySet. Define XView.model, XView.queryset,
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM