Skip to content

Instantly share code, notes, and snippets.

@sergray
Created January 19, 2011 16:17
Show Gist options
  • Save sergray/786384 to your computer and use it in GitHub Desktop.
Save sergray/786384 to your computer and use it in GitHub Desktop.
Amazing Django ORM extra SQL for Postgres
>>> tasks = Task.objects.\
... extra(select = {'t': "date_trunc('minute', task_task.date_created)"}).\
... values('t').annotate(Count('id')).order_by('t')
>>> print "\n".join("%(t)s | %(id__count)d" % t for t in tasks)
2011-01-19 11:26:00 | 767
2011-01-19 11:27:00 | 613
2011-01-19 11:28:00 | 522
2011-01-19 11:29:00 | 721
2011-01-19 11:30:00 | 701
2011-01-19 11:31:00 | 679
2011-01-19 11:32:00 | 732
2011-01-19 11:33:00 | 665
2011-01-19 11:34:00 | 682
2011-01-19 11:35:00 | 666
2011-01-19 11:36:00 | 749
2011-01-19 11:37:00 | 727
2011-01-19 11:38:00 | 422
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment