Django - вернуть список словарей
Я новичок, только начинаю работать с django. Я работаю над функцией, которая возвращает sub_total, tax_amount и grand_total. Я пробовал, но она не работает так, как должна. Я думаю, что параметры не верны или может быть что-то еще. Пожалуйста, рассмотрите это и предоставьте полное решение, если сможете. Также я пытаюсь выполнить запрос на Postman.
Правильно ли это:
def branch_report(request, params):
# branches = BusinessBranch.objects.filter(is_removed=False)
# orders = Order.objects.filter(is_removed=False)
branches = BusinessBranch.objects.filter(is_removed=False).annotate(
sub_total=Sum('orders__sub_total', filter=Q(orders__is_removed=False)),
grand_total=Sum('orders__grand_total', filter=Q(orders__is_removed=False)),
tax_amount=Sum('orders__tax_amount', filter=Q(orders__is_removed=False)),
).order_by('name')
branch_list = []
for branch in branches:
branch_obj = {}
branch_list_in = list(branch.business_branch.filter(is_removed=False).annotate(
sub_total=Sum('business_branch__sub_total', filter=Q(business_branch__is_removed=False)),
tax_amount=Sum('business_branch__tax_amount', filter=Q(business_branch__is_removed=False)),
grand_total=Sum('business_branch__grand_total', filter=Q(business_branch__is_removed=False)).values('name',
'id',
'sub_total',
'tax_amount',
'grand_total',)).order_by('name')
)
branch_obj['sub_total'] = branch.sub_total
branch_obj['tax_amount'] = branch.tax_amount
branch_obj['grand_total'] = branch.grand_total
branch_obj['id'] = branch.id
branch_obj['name'] = branch.name
branch_obj['branch_list_in'] = branch_list_in
branch_list.append(branch_obj)
all_total = Order.objects.filter(is_removed=False).aggregate(
all_sub_total=Sum('sub_total', filter=Q(is_removed=False)),
all_tax_amount=Sum('tax_amount', filter=Q(is_removed=False)),
all_grand_total=Sum('grand_total', filter=Q(is_removed=False)),
)
all_obj = {}
all_obj['all_sub_total'] = all_total['all_sub_total']
all_obj['all_tax_amount'] = all_total['all_tax_amount']
all_obj['all_grand_total'] = all_total['all_grand_total']
return response_format(SUCCESS_CODE, SUCCESSFUL_RESPONSE_MESSAGE, {'branches': branch_list, 'all_total': all_obj})
Я хочу, чтобы он возвращал следующее:
{
"code": 200,
"message": "Request was Successfull",
"data": {
"branches": [
{
"branch_list": [
{
"name": "Wahdat",
"id": 21,
"sub_total": null,
"tax_amount": null,
"grand_total": null,
}
]
},
{
"branch_list": [
{
"name": "WAPDA",
"id": 17,
"sub_total": null,
"tax_amount": null,
"grand_total": null,
}
]
}
],
"all_total": {
"all_sub_total": null,
"all_tax_amount": null,
"all_grand_total": null
}
}
}