简体   繁体   English

Django 统计今天的登录用户数

[英]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:我的管理界面如下所示:

在此处输入图片说明

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.它给了我 0 因为日期/时间格式不同。 ie 2016-06-01 14:58:29.079000+00:00即 2016-06-01 14:58:29.079000+00:00

How can I get that count on my admin interface somewhere?我怎样才能在我的管理界面的某个地方得到那个计数?

You have to get the date from the timezone.now() and then use filter 'startswith' to filter by date:您必须从 timezone.now() 获取日期,然后使用过滤器 'startswith' 按日期过滤:

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除了将此列添加到您的 Django 管理界面,您还可以使用 Django admin检查此自定义列

我认为这应该有效:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM