如何在Django中使用日期范围和求和以及排序依据

cclgggtu  于 2022-12-24  发布在  Go
关注(0)|答案(1)|浏览(126)

我正在处理一个项目,其中我有一个收入模型,该模型在其他字段中具有说明,如下所示。说明是一个选择字段,我希望使用“日期范围”来按每个说明求和。即,我希望对每个说明的所有金额求和,并在HTML模板中显示它们的合计。
下面是我已经尝试过的,但我得到了错误,显示为ValueError: too many values to unpack (expected 2)
型号代码:

CATEGORY_INCOME = (
('Photocopy', 'Photocopy'),
('Type & Print', 'Type & Print'),
('Normal Print', 'Normal Print'),
('Color Print', 'Color Print'),
('Passport', 'Passport'),
('Graphic Design', 'Graphic Design'),
('Admission Check', 'Admission Check'),
('Lamination', 'Lamination'),
('Document Scan', 'Document Scan'),
('Email Creation', 'Email Creation'),
('Email Check', 'Email Check'),
('Online Application', 'Online Application'),
('Agreement Form', 'Agreement Form'),
('Envelope / Binding Film', 'Envelope / Binding Film'),
('Web Development ', 'Web Development'),
)

class Income(models.Model): 
    description = models.CharField(max_length=100, choices=CATEGORY_INCOME, null=True)
    staff = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    amount = models.PositiveIntegerField(null=False)
    date = models.DateField(auto_now_add=False, auto_now=False, null=False)
    addedDate = models.DateTimeField(auto_now_add=True)

    class Meta: 
        verbose_name_plural = 'Income Sources'

    def __str__(self):
        return self.description

视图代码

def generate_reports(request):
    searchForm = IncomeSearchForm(request.POST or None)
    searchExpensesForm = ExpensesSearchForm(request.POST or None)
    if request.method == "POST" and searchForm.is_valid() and searchExpensesForm.is_valid():
        listIncome = Income.objects.filter(date__range=[searchForm['start_date'].value(),searchForm['end_date'].value()])
        total_income_passport = listIncome.values('description').order_by('description').annotate(total = Sum('amount')).get('total') or 0
        context = { 'total_income_passport':total_income_passport, }
        return render(request, 'cashier/print_report.html', context)
    else:
        listIncome = Income.objects.all()
    context = { 'listIncome ':listIncome , }
    return render(request, 'cashier/gen_report.html', context)

请注意,我没有问题的日期范围,但我的问题是与总和的每一个描述在模型中。您的亲切回答将不胜感激。谢谢

ekqde3dh

ekqde3dh1#

下面是我的版本,下面是文档:

from django.db.models import Sum

def generate_report(request):
    if request.method == 'POST':
        income_form = IncomeSearchForm(request.POST or None)
        expense_form = ExpensesSearchForm(request.POST or None)

        if income_form.is_valid() and expense_form.is_valid():
            start_date = income_form.cleaned_data['start_date']
            end_date = income_form.cleaned_data['end_date']

            grouped_incomes = Income.objects.filter(date__range=[start_date,end_date]).order_by('description')
            grouped_incomes_with_total = grouped_incomes.values('description').annotate(total = Sum('amount')).order_by()

            context = {'incomes': grouped_incomes_with_total}
            return render(request, 'print_report.html', context)

    else:
        income_form = IncomeSearchForm()
        expense_form = ExpensesSearchForm()

    context={
        'income_form': IncomeSearchForm,
        'expense_form': ExpensesSearchForm,
    }

    return render(request, 'generate_report.html', context)

相关问题