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

  • Home
  • SEARCH
  • 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 3235816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:33:00+00:00 2026-05-17T17:33:00+00:00

I’m having a hard time wrapping my head around this Django query. I could

  • 0

I’m having a hard time wrapping my head around this Django query. I could probably figure this out with SQL (and maybe I’ll have to), but I was wondering if there is a way to do it with Django objects.

The model looks something like this (simplified for clarity):

class Stat(model.Models):
    entry_date = models.DateTimeField()
    quantity = models.PositiveIntegerField()
    user = models.ForeignKey(User)

I need to return the sum (using annotate and sum, I’m guessing) of all the most recently added quantities by a user, grouped by month. It’s a little hard to explain, but the quantity is not cumulative — I only need to deal with the most recent records for a given user and a given month, and then I need to sum those quantities, and group them by month. If more explanation is needed, please say so.

UPDATE:

Here’s some rough psudo-code, as requested. This code is not necessarily what I would expect, but it’s roughly what I could do if, but in a slow, programmatic way. I’m hoping there is a way to do this via a single query for the sake of speed. (BTW, this is based on Manoj Govindan’s code below.)

year = list_of_months_that_have_stats
users = all_users_in_database
for months in year:
    for user in users:
        sum = Stat.object.filter(user=user, entry_date__month=month).order_by('-entry_date')[0].aggregate(sum=Sum('quantity'))
        final_output[month] = sum

Also, notice that I’m trying to get the very last record for the given month. I’m doing this with order_by, but as far as I know this won’t work — it’s just an illustration.

The code above won’t work, of course. The missing piece of the puzzle is the descending order_by that gets only the first item in the query.

  • 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-05-17T17:33:01+00:00Added an answer on May 17, 2026 at 5:33 pm

    I am not sure I understand exactly what you want. See my answer below and correct me if I am not getting it right.

    I only need to deal with the most recent records for a given user and a given month, and then I need to sum those quantities, and group them by month.

    (Emphasis added). If this is what you need, then you can use filter in combination with aggregate, as shown below.

    from django.db.models import Sum
    q = Stat.objects.filter(user = user, entry_date__month = 10).aggregate(
        sum = Sum('quantity'))
    print q
    # {'sum': 14}
    

    The filter conditions ensure that you have the required user and that the month part of the entry_date matches the month you want (in this case October, therefore 10). You can then Sum the quantity for the user/month combination.

    Update

    If you want the sum for an user per each month (I am guessing here that this is what you meant by “group them by month”) then you can try something like this:

    select = dict(month = 'extract(month from entry_date)')
    q = Stat.objects.filter(user = user).extra(
          select = select).values('month').annotate(
          sum = Sum('quantity'))
    
    print q
    # [{'sum': 14, 'month': 10.0}, {'sum': 6, 'month': 9.0}]
    

    Here is a quick explanation.

    1. extra is used to specify that you want to extract the month part from the date. This is database specific. In my case the syntax extract(month from entry_date) is specific to Postgresql.

    2. Use values to mention that you only need month. This is important. If you leave out the values part all fields are fetched and you won’t get the effect you need.

    3. Finally annotate each record with the Sum('quantity'). This returns the sum for each month.

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

Sidebar

Related Questions

No related questions found

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.