我有这个模型:
class Transfer(models.Model):
transfer_code = models.CharField(max_length=30, blank=True, null=True)
sender_name = models.CharField(max_length=30, blank=True, null=True)
amount = models.IntegerField(blank=True, null=True)
recipient_name = models.CharField(max_length=30, blank=True, null=True)
time_sent = models.DateTimeField(auto_now_add=True, auto_now=False)
received = models.BooleanField(default=False)
time_received = models.DateTimeField(auto_now_add=False, auto_now=False, null=True)
def __unicode__(self):
return self.transfer_code
这是我想计算表中总金额的视图:
def listtransfersall(request):
title = 'ALL TRANSFERS'
queryset = Transfer.objects.all()
for instance in queryset:
total = 0
total += instance.amount
print total
context = {
"title": title,
"queryset": queryset,
"total": total,
}
return render(request, "listtransfersall.html",context)
这将单独打印表格中的金额。我怎样才能得到总数并将其分配给总变量?