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
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))