Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to Count quantity of my QuerySet. Django

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading