Thinker
Thinker

Reputation: 5366

Django count today's logged in users

I want to show on my admin interface the count of the users those did log in today. My admin interface looks like following:

enter image description here

When I print from my view

 count = User.objects.filter(last_login=timezone.now()).count()

it gives me 0 as both the date/time format are different. i.e. 2016-06-01 14:58:29.079000+00:00

How can I get that count on my admin interface somewhere?

Upvotes: 2

Views: 1928

Answers (2)

Sameer Verma
Sameer Verma

Reputation: 26

I think this should work:

count = User.objects.filter(last_login__startswith=timezone.now().date()).count()

Upvotes: 0

ezdookie
ezdookie

Reputation: 1677

You have to get the date from the timezone.now() and then use filter 'startswith' to filter by date:

count = User.objects.filter(last_login__startswith=timezone.now().date()).count()

In addition to add this column to your Django Admin Interface you can check this Custom columns using Django admin

Upvotes: 2

Related Questions