In an ideal world, you can write:
archive = Articles.objects.values('created__year').annotate(archive_count=Count('created')).order_by()
to get the desired results. Unfortunately, django does not support anything other than exact field names as argument values ββ()
archive = Articles.objects.values('created').aggregate(archive_count=Count('created'))
may not work because the values ββ("created") give you a dictionary of all unique values ββfor "created", which consists of more than a "year".
If you really want to do this with a single ORM call, you will have to use an extra function and write your own custom SQL. Otherwise, Justin's answer should work well.
source share