Django - Count user growth each month
from django.db.models import Count
from django.contrib.auth.models import User
qs = (User.objects.all().
extra(select={
'month': "EXTRACT(month FROM date_joined)",
'year': "EXTRACT(year FROM date_joined)",
}).
values('month', 'year').
annotate(count_items=Count('date_joined')))
qs = qs.order_by('date_joined')
print "month,count,total"
total = 0
for item in qs:
total = total + item['count_items']
if item['year'] < 2013: continue
print "%02d-%s,%s,%s" % (item['month'], item['year'], item['count_items'], total)
based on http://stackoverflow.com/questions/21837227/annotate-group-dates-by-month-year-in-django
Written by k4ml
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Related Tags
#django
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#