Django - get distinct dates from timestamp

前端 未结 2 1993
感情败类
感情败类 2021-02-08 19:12

I\'m trying to filter users by date, but can\'t until I can find the first and last date of users in the db. While I can have my script filter out dups later on, I want to do it

2条回答
  •  既然无缘
    2021-02-08 19:44

    When doing reports on larger datasets itertools.group_by might be too slow. In those cases I make postgres handle the grouping:

    truncate_date = connection.ops.date_trunc_sql('day','timestamp')
    qs = qs.extra({'date':truncate_date})
    return qs.values('date').annotate(Sum('amount')).order_by('date')
    

提交回复
热议问题