我正在处理一个项目,其中我有一个收入模型,该模型在其他字段中具有说明,如下所示。说明是一个选择字段,我希望使用“日期范围”来按每个说明求和。即,我希望对每个说明的所有金额求和,并在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)
请注意,我没有问题的日期范围,但我的问题是与总和的每一个描述在模型中。您的亲切回答将不胜感激。谢谢
1条答案
按热度按时间ekqde3dh1#
下面是我的版本,下面是文档: