2

I have managed to create a QuerySet of what I want so far, but I am unable to include the .count () statement in it. I do know that I can use len (students), but I would like to stick to the Django ORM.

student_count = Student.objects.filter(last_online__range=[last week, this week])

How do I do that?

Astrid TAW
  • 3
  • 1
  • 3
uber
  • 4,163
  • 5
  • 26
  • 55

2 Answers2

2

You can use count()

Student.objects.filter(last_online__range=[last week, this week]).count()
Mahmoud Adel
  • 1,262
  • 2
  • 13
  • 23
1

you can use aggregate() to count, like this:

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.

Max
  • 836
  • 6
  • 13