views:

19

answers:

1

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.

A: 

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.

Manoj Govindan
Thanks for the answer. That's more or less what I need, except for one big difference: I need the very last record added, not simply a record with a specific date. So for August I need the most recent record added by a certain user. For September I need the same thing. Again, this is hard to explain, but thanks for your willingness to help.
Jude Osborn
To explain further, what I mean by "last record added" is really the most recent record. Basically .order_by('-entry_date')[0], if you get what I mean.
Jude Osborn