Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - get distinct dates from timestamp

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 from the outset using Django's distinct since it significantly reduces. I tried

User.objects.values('install_time').distinct().order_by()

but since install_time is a timestamp, it includes the date AND time (which I don't really care about). As a result, the only ones it filters out are dates where we could retrieve multiple users' install dates but not times.

Any idea how to do this? I'm running this using Django 1.3.1, Postgres 9.0.5, and the latest version of psycopg2.

EDIT: I forgot to add the data type of install_time:

install_time = models.DateTimeField()

EDIT 2: Here's some sample output from the Postgres shell, along with a quick explanation of what I want:

 2011-09-19 00:00:00
 2011-09-11 00:00:00
 2011-09-11 00:00:00 <--filtered out by distinct() (same date and time)
 2011-10-13 06:38:37.576
 2011-10-13 00:00:00 <--NOT filtered out by distinct() (same date but different time)

I am aware of Manager.raw, but would rather user django.db.connection.cursor to write the query directly since Manager.raw returns a RawQuerySet which, IMO, is worse than just writing the SQL query manually and iterating.

like image 368
Edwin Avatar asked Dec 21 '11 21:12

Edwin


1 Answers

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')
like image 137
tback Avatar answered Nov 17 '22 05:11

tback