把它分成两个步骤objs = StoreVideoEventSummary.objects.filter(Timestamp__range=(start_time, end_time),
Customer__id=customer_id,
Store__StoreName=store)\
.order_by("Timestamp")
def date_hour(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime("%x %H")
groups = itertools.groupby(objs, lambda x:date_hour(x.Timestamp))
#since groups is an iterator and not a list you have not yet traversed the list
for group,matches in groups: #now you are traversing the list ...
print group,"TTL:",sum(1 for _ in matches)
这允许您按几个不同的标准进行分组
如果你只想要小时而不考虑日期,只需更改date_hour
^{pr2}$
如果你想按一周中的哪一天分组,你只需使用def date_hour(timestamp):
return datetime.datetime.fromtimestamp(timestamp).strftime("%w %H")