1👍
✅
Thanks to @PTomasz comment, I’ve added Window expression to my query.
So the final query looks like this:
groups = Group.objects.prefetch_related(
Prefetch("facts",
queryset=Facts.objects.annotate(
sum_weight=Window(
expression=Sum('weight'),
partition_by=[F('date')],
order_by=OrderBy(F('date'))))
.distinct("date"))
)
Without .distinct("date")
objects were still duplicating, but with right sum_weight
(both had 1500).
With it everyting is good.
Source:stackexchange.com