[Answered ]-Django get row count by date

2👍

Maybe you have defined a default ordering in your PropertyImpression model?

In this case, you should add order_by() before annotate to reset it :

prop_imps = PropertyImpression.objects.values('imp_date').order_by() \
                                      .annotate(count=Count('id'))

It’s explained in Django documentation here:

Fields that are mentioned in the order_by() part of a queryset (or which are used in the default ordering on a model) are used when selecting the output data, even if they are not otherwise specified in the values() call. These extra fields are used to group “like” results together and they can make otherwise identical result rows appear to be separate. This shows up, particularly, when counting things.

Leave a comment