【发布时间】:2017-09-24 20:42:51
【问题描述】:
在我的 django 项目中,可以使用 CustomerProfile.objects.all() 向应用程序中的每个客户显示,并使用
In [12]: cust = CustomerProfile.objects.get(pk=100)
In [13]: cust.user.date_joined
Out[13]: datetime.datetime(2017, 7, 28, 14, 43, 51, 925548)
In [14]: cust
Out[14]: <CustomerProfile: FistName LastName's customer profile>
根据创建日期,我想列出每天、每周、每月或每年创建了多少客户。结果的一个例子可能是
...
week 28 : [ 09/07/2017 - 15/07/2017 ] - Count : 201 customers
...
我可能需要一个范围start_date 和end_date,我们将在其中列出此类信息。 start_date 将是创建第一个客户的日期,而创建的start_week 将是此 first_date 的那一周。显然,end_date 是最后一个客户创建的日期,last_week 是这个end_date 的星期。
例如,如果我在下拉菜单中选择Per week 并在表单中按Apply,我想将信息发送到我的模型,这样我就可以编写我解释的代码。
到目前为止,我知道如何计算两个日期范围内的客户数量:
CustomerProfile.objects.filter(user__date_joined__range=["2017-09-03", "2017-09-09"])
我怎样才能在这里更新日期,以便我们可以得到 start_week 和 end_week 之间的列表?是否存在找到first_date 和last_date 的有效算法(具有良好的代码性能)?如果可能的话,将不胜感激。
【问题讨论】:
-
我所有的日期都是 datetime.datetime(...) 的形式。重构所有代码应该很困难。你为什么不写一个完整的答案? ;)