Well, i have the following 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, i.e. make a Count function.
Can someone help me with the syntax?
>Solution :
You can use the .count() method on a queryset to get the number of items in the queryset. Here’s the updated code:
employees_birthday = Employees.objects.filter(EmployeeDataNasc__gte=first_day, EmployeeDataNasc__lte=last_day)
employee_count = employees_birthday.count()
In this example, employees_birthday is the queryset containing all employees born between the given dates. The .count() method returns the number of items in the queryset, which is stored in the variable employee_count.
You can then use the employee_count variable in your code to display the number of employees, for example:
print("Number of employees born between {} and {}: {}".format(first_day, last_day, employee_count))