Django获得昨天的登录计数

时间:2016-08-16 17:41:19

标签: django django-rest-framework

我有一个视图,我正在获取有关登录信息的一些分析数据,如下所示:

def get_analytics(request):

    total_users = User.objects.all().count()
    # Number of users who dogged in once to our system today
    today_login_count= User.objects.filter(last_login__startswith=timezone.now().date()).count()

    #Number of users who dogged in once to our system in the last week
    today = timezone.now().date() + timezone.timedelta(days=1)
    last_week = timezone.now().date() - timezone.timedelta(days=7)
    last_month = timezone.now().date() - timezone.timedelta(days=30)
    week_login_count = User.objects.filter(last_login__range=(last_week, today)).count()

    #Number of users who dogged in once to our system in the last month
    month_login_count = User.objects.filter(last_login__range=(last_month, today)).count()

    data = {}
    data['total_users'] = total_users
    data['today_login_count'] = today_login_count
    data['week_login_count'] = week_login_count
    data['month_login_count'] = month_login_count

    return JSONResponse(data)

我想获得昨天登录 的用户总数。我怎么查询呢?

1 个答案:

答案 0 :(得分:1)

User.objects.filter(
    last_login__date=timezone.now().date() - timedelta(days=1)
).count()