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
[0] https://docs.djangoproject.com/en/3.0/ref/models/expressions/#window-functions
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 centsAgneseOn 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 foundThis gave me a list of dates. Now I'm blocked to sum them up per month.
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/c2cbe3ac-9acd-44cd-a48b-24d74f75cb09%40googlegroups.com.
No comments:
Post a Comment