Tuesday, February 25, 2020

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

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 <mariobisiani@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-users+unsubscribe@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/CACXuh-Ryg0C6W%2Br7U%2BZqJywcmv9-o1MiwKCarm1dmT%3DK%2B5quHQ%40mail.gmail.com.

No comments:

Post a Comment