Большое спасибо всем. Наконец-то я нашел решение своего вопроса и подумал о том, чтобы опубликовать его для справки других.
Это мои запросы:
group_debit_positive = GroupBase.objects.filter(base_group__group_ledger__company=company, is_debit__exact='Yes', base_group__group_ledger__closing_balance__gt=0).annotate(
total_debit_positive_opening=Sum(0, output_field=FloatField()),
total_debit_negative_opening=Sum(0, output_field=FloatField()),
total_credit_positive_opening=Sum(0, output_field=FloatField()),
total_credit_negative_opening=Sum(0, output_field=FloatField()),
total_debit_positive=Coalesce(
Sum('base_group__group_ledger__closing_balance'), Value(0)),
total_debit_negative=Sum(0, output_field=FloatField()),
total_credit_positive=Sum(0, output_field=FloatField()),
total_credit_negative=Sum(0, output_field=FloatField())).exclude(name__exact='Primary').exclude(name__exact='Current Assets').values(
'name',
'total_debit_positive',
'total_debit_negative',
'total_credit_positive',
'total_credit_negative')
group_debit_negative = GroupBase.objects.filter(base_group__group_ledger__company=company, is_debit__exact='Yes', base_group__group_ledger__closing_balance__lt=0).annotate(
total_debit_positive_opening=Sum(0, output_field=FloatField()),
total_debit_negative_opening=Sum(0, output_field=FloatField()),
total_credit_positive_opening=Sum(0, output_field=FloatField()),
total_credit_negative_opening=Sum(0, output_field=FloatField()),
total_debit_positive=Sum(0, output_field=FloatField()),
total_debit_negative=Coalesce(
Sum('base_group__group_ledger__closing_balance'), Value(0)),
total_credit_positive=Sum(0, output_field=FloatField()),
total_credit_negative=Sum(0, output_field=FloatField())).exclude(name__exact='Primary').exclude(name__exact='Current Assets').values(
'name',
'total_debit_positive',
'total_debit_negative',
'total_credit_positive',
'total_credit_negative')
group_credit_positive = GroupBase.objects.filter(base_group__group_ledger__company=company, is_debit__exact='No', base_group__group_ledger__closing_balance__gt=0).annotate(
total_debit_positive_opening=Sum(0, output_field=FloatField()),
total_debit_negative_opening=Sum(0, output_field=FloatField()),
total_credit_positive_opening=Sum(0, output_field=FloatField()),
total_credit_negative_opening=Sum(0, output_field=FloatField()),
total_debit_positive=Sum(0, output_field=FloatField()),
total_debit_negative=Sum(0, output_field=FloatField()),
total_credit_positive=Coalesce(
Sum('base_group__group_ledger__closing_balance'), Value(0)),
total_credit_negative=Sum(0, output_field=FloatField())).exclude(name__exact='Primary').exclude(name__exact='Current Assets').values(
'name',
'total_debit_positive',
'total_debit_negative',
'total_credit_positive',
'total_credit_negative')
group_credit_negative = GroupBase.objects.filter(base_group__group_ledger__company=company, is_debit__exact='No', base_group__group_ledger__closing_balance__lt=0).annotate(
total_debit_positive_opening=Sum(0, output_field=FloatField()),
total_debit_negative_opening=Sum(0, output_field=FloatField()),
total_credit_positive_opening=Sum(0, output_field=FloatField()),
total_credit_negative_opening=Sum(0, output_field=FloatField()),
total_debit_positive=Sum(0, output_field=FloatField()),
total_debit_negative=Sum(0, output_field=FloatField()),
total_credit_positive=Sum(0, output_field=FloatField()),
total_credit_negative=Coalesce(Sum('base_group__group_ledger__closing_balance'), Value(0))).exclude(name__exact='Primary').exclude(name__exact='Current Assets').values(
'name',
'total_debit_positive',
'total_debit_negative',
'total_credit_positive',
'total_credit_negative')
Сначала я вернусьнабор запросов, содержащий словарь.
Затем я выполнил объединение запросов, как в моем вопросе
final_queryset = group_debit_positive.union(
group_debit_negative, group_credit_positive, group_credit_negative, all=True).order_by('name')
Затем я отфильтровал группу запросов по их name
, чтобы избежать дублирования результатов (как аннотации). / агрегация не может быть выполнена в объединении наборов запросов)
grouped_group_name = itertools.groupby(
final_queryset, key=lambda x: (x['name']))
Тогда
result = []
for group_key, group_values in grouped_group_name:
positive_debit = decimal.Decimal(0.0)
negative_debit = decimal.Decimal(0.0)
positive_credit = decimal.Decimal(0.0)
negative_credit = decimal.Decimal(0.0)
# Storing each value into a variable to render a list later
for each_group in group_values:
positive_debit += decimal.Decimal(
each_group['total_debit_positive'])
negative_debit += decimal.Decimal(
each_group['total_debit_negative'])
positive_credit += decimal.Decimal(
each_group['total_credit_positive'])
negative_credit += decimal.Decimal(
each_group['total_credit_negative'])
# Making a list of items fetched to render in Django Template
result.append({
'group_name': group_key,
'positive_debit': round(positive_debit, 2),
'negative_debit': round(negative_debit, 2),
'positive_credit': round(positive_credit, 2),
'negative_credit': round(negative_credit, 2),
})
Таким образом, я смог найти решение моей проблемы.
Iзнаю, это немного длительный процесс.
Спасибо всем за помощь.