1

I have a Post model that has a datetime.date field for posted_date. I need to find how many posts are made by a user on each day. Creating a query for each day seems silly.

I couldn't figure out how to make the aggregation API query using annotate.

Is

Post.objects.filter(author=someuser).annotate(dailycount=Count('posted_day'))

the correct way? Then, how do I use this to get number of posts for a particular day?

My model is:

class Post(models.Model):
    posted_day=models.DateField(default=date.today)
    author=models.ForeignKey(User,null=True)
agf
  • 171,228
  • 44
  • 289
  • 238
damon
  • 8,127
  • 17
  • 69
  • 114

1 Answers1

1

You are almost there. You need two additional clauses:

day_counts = Post.objects.filter(author=someuser).values('posted_day').annotate(
                                       dailycount=Count('posted_day')).order_by()

The values('posted_day') enables the grouping, and the empty order_by ensures the results are ordered by posted_day so the default ordering doesn't interfere.

The clearest documentation of this seems to be in the Django Aggregation docs section on the Order of annotate() and values() clauses.

values returns a list of dicts like:

[{'posted-day': 'the-first-day', 'dailycount': 2}, . . . ,
 {'posted-day': 'the-last-day', 'dailycount': 3}]

so if you wanted the last day the user posted, it would be the last item in the list:

last_day_dict = day_counts[-1]
date = last_day_dict['posted_day']
count = last_day_dict['dailycount']

You can then compare date to today() to see if they match, and if they don't the user didn't post today, and if he did, he posted count times.

agf
  • 171,228
  • 44
  • 289
  • 238
  • thanks for the reply..from the returned queryset ,how do I find the number of posts on `day1` which is say `datetime.date.today()` ? – damon Apr 14 '12 at 20:20
  • @damon I added more info to my answer. – agf Apr 14 '12 at 20:29