Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:43:41+00:00 2026-06-02T04:43:41+00:00

I have a Post model that has a datetime.date field for posted_date . I

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:43:42+00:00Added an answer on June 2, 2026 at 4:43 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a post model that has many tags through taggings. let's say: @posts
I have a post controller that has many comments. The post model has a
I have a story model that has many posts. I list the stories in
Say you have a Post model that has a :title , :author , :content
I have a Post model that has a polymorphic association with a Vote model:
I have a Post model object that has reference to a parent object. The
I have a model that has ratings in it for an post. I want
I have two models, Article and Post that both inherit from a base model
I have a User model, a Post model, and an Interest model. User has_many
I have two models related one-to-many: a Post and a Comment : class Post(models.Model):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.