I annotate two querysets
queryset = Statistic.objects.all()
daily_statistic = queryset.filter(
created__gte=last_week_date,
statistic_type=StatisticType.DAILY
).values(
*self.ATTRS_TO_GROUP_BY
).annotate(
character_count=Sum('character_count'),
word_count=Sum('word_count'),
segment_count=Sum('segment_count'),
token_in=Sum('token_in'),
token_out=Sum('token_out')
)
today_date = timezone.localdate()
today_statistic = queryset.filter(
created__date=today_date,
statistic_type=StatisticType.DEFAULT
)
today_statistic = today_statistic.values(
*self.ATTRS_TO_GROUP_BY
).annotate(
character_count=Sum('character_count'),
word_count=Sum('word_count'),
segment_count=Sum('segment_count'),
token_in=Sum('token_in'),
token_out=Sum('token_out')
)
But then I need to unit the querysets described abowe and perform "group by" by ATTRS_TO_GROUP_BY to obtain weekly statistics.
When I try this with the following code
combined_statistic = daily_statistic.union(today_statistic)
combined_statistic = combined_statistic.annotate(
total_character_count=Sum('character_count'),
total_word_count=Sum('word_count'),
total_segment_count=Sum('segment_count'),
total_token_in=Sum('token_in'),
total_token_out=Sum('token_out')
)
I obtain
django.db.utils.NotSupportedError: Calling QuerySet.annotate() after union() is not supported.