Tuesday, February 25, 2020

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

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.

No comments:

Post a Comment