我正在使用Django 1.8.3和Python 3.4.3
我有一个模型,它返回一个对象列表到我的模板,其中包含' for'环;其中一张表是针对' sales'。我想创建一个标签,它是所有'销售'并使用标记将total_sales插入到我的模板中。前提是不必手动将所有列添加到一起以获得总数,只需在页面上有一个自动计算并显示总数的部分。
以下是我当前的型号/视图。任何帮助是极大的赞赏。谢谢。
model.py
class Traffic(models.Model):
date = models.DateField()
traffic = models.PositiveIntegerField(null=True)
day_of_week = models.CharField(max_length=255, null=True)
ifs_signups = models.IntegerField(null=True)
campaigns = models.IntegerField(null=True)
sales = models.PositiveIntegerField(null=True)
def month(self):
return self.date.strftime("%B")
def quarter(self):
item = self.date.strftime("%B")
q1 = (["January", "February", "March"])
q2 = (["April", "May", "June"])
q3 = (["July", "August", "September"])
q4 = (["October", "November", "December"])
if item in q1:
return "1st Quarter"
elif item in q2:
return "2nd Quarter"
elif item in q3:
return "3rd Quarter"
elif item in q4:
return "4th Quarter"
else:
return "No Date Specified"
def conversions(self):
conversion = self.sales / self.traffic
return "%r" % conversion
views.py(def _total_sales(self):在我的尝试之下)
from django.db.models import Sum
from .models import Traffic, Email
class TrafficListView(ListView):
model = Traffic
template_name = 'dashboard/pages/traffic.html'
def get_context_data(self, **kwargs):
context = super(TrafficListView, self).get_context_data(**kwargs)
return context
def _total_sales(self):
return self.objects.aggregate(total_sales=Sum('sales'))