Skip to content

Instantly share code, notes, and snippets.

@makmac213
Created March 28, 2016 15:02
Show Gist options
  • Save makmac213/dca600fe4d6c415dcac3 to your computer and use it in GitHub Desktop.
Save makmac213/dca600fe4d6c415dcac3 to your computer and use it in GitHub Desktop.
Django Queries
from django.contrib.auth.models import User
from django.db.models import Count
qs = (User.objects.all().
extra(select={
'm': "EXTRACT(month FROM date_joined)",
'd': "EXTRACT(day FROM date_joined)",
'y': "EXTRACT(year FROM date_joined)",
}).
values('d', 'm', 'y').
annotate(count_items=Count('date_joined')))
qs = qs.order_by('date_joined')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment