items = Foo.objects.filter(createdate__lte=datetime.datetime.today(), createdate__gt=datetime.datetime.today()-datetime.timedelta(days=30)).\ values('createdate').annotate(count=Count('id'))
This (1) filtering results will contain the last 30 days, (2) select only the created field and (3) count the identifier by grouping all selected fields (i.e. create). This will return a list of format dictionaries:
[ {'createdate': <datetime.date object>, 'count': <int>}, {'createdate': <datetime.date object>, 'count': <int>}, ... ]
EDIT:
I do not believe that there is a way to get all dates, even those with count == 0 , only with SQL. You will need to insert each missing date through python code, for example:
import datetime
source share