[Answer]-Can not count fields correctly in a Django view

1👍

Try this:

Download.objects.filter(download_date__range=[date_1, date_2]).values('song').annotate(downloads = Count('id'))

0👍

You must change order_by. Explained here https://docs.djangoproject.com/en/dev/topics/db/aggregation/#interaction-with-default-ordering-or-order-by

res = Download.objects.filter(download_date__range=[date_1, date_2])\
              .annotate(downloads=Count('song'))\
              .values('downloads', 'song')\
              .order_by()

Leave a comment