Last Updated: February 25, 2016
·
4.45K
· k4ml

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