Get last instance object per each month and sum per month in Django

38 views
Skip to first unread message

Mario Bisiani

unread,
Feb 25, 2020, 3:08:14 PM2/25/20
to Django users

I tryed to search for answer but nothing yet can help me.

I have one class model Activity and a class model Status :

class Status(models.Model):
    activity = models.ForeignKey(Activity, on_delete=models.CASCADE, related_name="all_status")
    status_date = models.DateField()
    actual_progress = models.DecimalField(max_digits=3, decimal_places=2)

I need to get last 'status_date' for each activity for each month and sum up its actual_progress.

Can't find after many research a way out.

Update

I found a way to get latest status per activity helped here Django - get latest object in each relation:

@classmethod
    def get_latest_by_activity_ids(cls, activity_id):
        found = []
        for aid in Activity.objects.all():
            found.append(cls.objects.filter(activity_id=aid).latest("status_date"))
        return found 

This gave me a list of dates. Now I'm blocked to sum them up per month.


https://stackoverflow.com/questions/60366457/get-last-instance-object-per-each-month-and-sum-per-month-in-django


Agnese Camellini

unread,
Feb 25, 2020, 5:55:49 PM2/25/20
to django...@googlegroups.com
You should search how to make a group by activity and a partition by month then take each partition and sum the record up, i don't know how to do it in Django orm but in sql is the most effective way to solve the issue and it's a lot let expensive than taking all the activities and then search in the status Table.
my 2 cents
Agnese

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/91d43812-6c7a-431d-9117-7a79519564fd%40googlegroups.com.

Simon Charette

unread,
Feb 25, 2020, 6:17:33 PM2/25/20
to Django users
If you don't want to operate on Activity instances annotations you can use the following queryset

Status.objects.annotate(
    status_month=TruncMonth('status_date'),
).order_by(
    'activity', 'status_month',
).values_list(
    'activity', 'status_month',
).annotate(
    progress_sum=Sum(actual_progress)
)

That will return an iterable of lists of the form [activity_id, status_month, progress_sum]

If you want to annotate your Activity instances you'll need to use window functions[0].

Simon

To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Mario Bisiani

unread,
Feb 26, 2020, 2:26:24 PM2/26/20
to Django users
Thank you a lot for your answer Simon! Your solution is very near to what I need. Next step to complete is to get latest date status per activity and then sum them up.  With your code I can get the list for every status. Thank a lot any way! I feel I'm next to the solution :-)

Mario Bisiani

unread,
Feb 26, 2020, 2:27:49 PM2/26/20
to Django users
Thank you Agnese , Grazie!

I will consider your prompt :-)
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages