Django - get distinct dates from timestamp

前端 未结 2 1992
感情败类
感情败类 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:48

    I've voted to close this since it's a dup of this question, so here's the answer if you don't want to visit the link, courtesy of nosklo.


    Create a small function to extract just the date: def extract_date(entity): 'extracts the starting date from an entity' return entity.start_time.date()

    Then you can use it with itertools.groupby:

    from itertools import groupby
    
    entities = Entity.objects.order_by('start_time')
    for start_date, group in groupby(entities, key=extract_date):
        do_something_with(start_date, list(group))
    

提交回复
热议问题