Skip to content Skip to sidebar Skip to footer

Group Objects By Dates

clicks = SellerClick.objects.extra({'date' : 'date(timestamp)'}).values('date').annotate(count=Count('timestamp')) The model has a datetime field called timestamp that was are usi

Solution 1:

I prefer to use annotate over extra

from django.db.models.expressions import RawSQL  

SellerClick.objects.annotate(
    date=RawSQL('date(date_joined)',[]),
).values('date').annotate(count=Count('date')))

Solution 2:

You've got everything but an initial queryset there. The extra sql you're passing doesn't include a select so you need to give it something to act on.

clicks = SellerClick.objects.all()
    .extra({'date' : "date(timestamp)"})
    .values('date')
    .annotate(count=Count('timestamp'))

Ref: StackOverflow: Count number of records by date in Django


Post a Comment for "Group Objects By Dates"