1๐
โ
Your total is calculated by the following line:
add = expense.objects.all().aggregate(Sum('IncludeTax'))['IncludeTax__sum']
It can be seen that you are not filtering the above queryset, hence the aggregate is performed on all the rows. To get the filtered queryset one can simply access it from the .qs
property of the filterset instance, hence you can change your view as follows:
def expense_table(request):
item = expense.objects.all().order_by('IncludeTax')
search_list = expense.objects.all().order_by('user__Site_Code', 'Date')
search_filter = expenseFilter(request.GET, queryset=search_list)
add = search_filter.qs.aggregate(Sum('IncludeTax'))['IncludeTax__sum']
return render(request, 'expense/MExpense.html', {'item': item, 'filter': search_filter, 'add': add})
Source:stackexchange.com