gpt4 book ai didi

django - 一天和一周的总用户数

转载 作者:行者123 更新时间:2023-12-02 19:01:28 25 4
gpt4 key购买 nike

我想获取一天和一周内加入的用户总数,我尝试如下

member_in_day = User.objects.exclude(is_staff=True)\
.annotate(day=TruncDay('date_joined')).values('date_joined') \
.annotate(total_members=Count('date_joined')) \
.order_by('-date_joined')
member_in_week = User.objects.exclude(is_staff=True)\
.annotate(week=TruncWeek('date_joined')).values('date_joined') \
.annotate(total_members=Count('date_joined')) \
.order_by('-date_joined')

模型是用户,我得到的两个查询的输出是相同的,而不是一天它也给出其他查询集。我如何将今天的总计和本周的总计添加到模板

<QuerySet [{'date_joined': datetime.datetime(2020, 12, 28, 16, 47, 40, 509134, tzinfo=<UTC>), 'total_members': 1}, {'date_joined': datetime.datetime(2020, 12, 23, 11, 8, 19, 241355, tzinfo=<UTC>), 'total_members': 1}, {'date_joined': datetime.datetime(2020, 12, 23, 11, 6, 14, 20673, tzinfo=<UTC>), 'total_members': 1}, {'date_joined': datetime.datetime(2020, 12, 22, 9, 25, 45, 14632, tzinfo=<UTC>), 'total_members': 1}, {'date_joined': datetime.datetime(2020, 12, 22, 9, 24, 25, 819750, tzinfo=<UTC>), 'total_members': 1}, {'date_joined': datetime.datetime(2020, 12, 20, 12, 7, 37, 59113, tzinfo=<UTC>), 'total_members': 1}]> members

最佳答案

您不应在截断后使用date_joined,而应使用dayweek,因此:

member_in_day = User.objects.exclude(is_staff=True).values(
<b>day=</b>TruncDay('date_joined')
).annotate(
total_members=Count('date_joined')
)<b>.order_by('-day')</b>

members_in_week相同:

member_in_week = User.objects.exclude(is_staff=True).values(
<b>week=</b>TruncWeek('date_joined')
).annotate(
total_members=Count('date_joined')
)<b>.order_by('-week')</b>

关于django - 一天和一周的总用户数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65519727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com