Wednesday, February 26, 2020

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

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 :-)

Il giorno mercoledì 26 febbraio 2020 00:17:33 UTC+1, Simon Charette ha scritto:
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


Le mardi 25 février 2020 17:55:49 UTC-5, Agnese Camellini a écrit :
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

On Tue, 25 Feb 2020 at 21:07, Mario Bisiani <mariob...@gmail.com> wrote:

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


--
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...@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8b8fe10e-a3d9-473e-8375-fac0086405e7%40googlegroups.com.

No comments:

Post a Comment