我有一个以下列格式输出数据的查询:
[["W/E 6/11/17", "Carter, J", 40.0],
["W/E 6/18/17", "Carter, J", 40.0],
["W/E 6/11/17", "Linn, K", 27.0],
["W/E 6/18/17", "Linn, K", 27.0],
["W/E 6/11/17", "Massey, S", 55.0],
["W/E 6/18/17", "Massey, S", 45.0]]
我的查询:
emp3 = (
Projectsummaryplannedhours.objects.values(
'employeename', 'displayval')
.order_by()
.filter(businessunit='a')
.filter(billinggroup__startswith='PLS - Pip')
.filter(Q(displayval=sunday2)|Q(displayval=sunday))
.annotate(plannedhours__sum=Sum('plannedhours'))
)
在我的模板中,我目前正在使用for循环,但它会返回列表中的所有项目,而不仅仅是第一个列表列表。
{% for x in emp3 %}
{{x.employeename}}
{{x.plannedhours__sum}}
我想要做的是遍历列表并以水平形式显示员工:W / E 6/11的值,W / E 6/18的值。
型号:
class Projectsummaryplannedhours(models.Model):
number = models.CharField(db_column='Number', max_length=50, blank=True, null=True) # Field name made lowercase.
description = models.CharField(db_column='Description', max_length=100, blank=True, null=True) # Field name made lowercase.
clientname = models.CharField(db_column='ClientName', max_length=100, blank=True, null=True) # Field name made lowercase.
department = models.CharField(db_column='Department', max_length=50, blank=True, null=True) # Field name made lowercase.
billinggroup = models.CharField(db_column='BillingGroup', max_length=50, blank=True, null=True) # Field name made lowercase.
businessunit = models.CharField(db_column='BusinessUnit', max_length=50, blank=True, null=True) # Field name made lowercase.
employeename = models.CharField(db_column='EmployeeName', max_length=50, blank=True, null=True) # Field name made lowercase.
displayval = models.CharField(db_column='DisplayVal', max_length=50, blank=True, null=True) # Field name made lowercase.
startofweek = models.DateTimeField(db_column='StartOfWeek', blank=True, null=True) # Field name made lowercase.
endofweek = models.DateTimeField(db_column='EndOfWeek', blank=True, null=True) # Field name made lowercase.
plannedhours = models.DecimalField(db_column='PlannedHours', max_digits=10, decimal_places=5, blank=True, null=True) # Field name made lowercase.
rateschedule = models.CharField(db_column='RateSchedule', max_length=50, blank=True, null=True) # Field name made lowercase.
classification = models.CharField(db_column='Classification', max_length=50, blank=True, null=True) # Field name made lowercase.
dollarsforecast = models.DecimalField(db_column='DollarsForecast', max_digits=10, decimal_places=5, blank=True, null=True) # Field name made lowercase.
deleted = models.NullBooleanField(db_column='Deleted') # Field name made lowercase.
datelastmodified = models.DateTimeField(db_column='DateLastModified', blank=True, null=True) # Field name made lowercase.
datecreated = models.DateTimeField(db_column='DateCreated', blank=True, null=True) # Field name made lowercase.
更新了查询:
emp3_list = Projectsummaryplannedhours.objects.values('employeename', 'displayval').order_by().filter(businessunit='a').filter(billinggroup__startswith='PLS - Pip').filter(Q(displayval=sunday2)|Q(displayval=sunday)).annotate(plannedhours__sum=Sum('plannedhours'))
emp3 = map(lambda x: {'date': x[0], 'employee_name': x[1], 'planned_hours': x[2]}, emp3_list)
我尝试过的每个查询:
def DesignHubR(request):
#emp1 = Projectsummaryplannedhours.objects.filter(employeename__startswith='Linn').values_list('endofweek').annotate(plannedhours__sum=Sum('plannedhours'))
day = datetime.datetime.today()
start = day - timedelta(days=day.weekday())
s1 = start + timedelta(days=6)
day2 = day + + timedelta(days=7)
start2 = day2 - timedelta(days=day.weekday())
s2 = start2 + timedelta(days=6)
sunday = datetime.datetime.strftime(s1, "W/E %#m/%#d/%y")
sunday2 = datetime.datetime.strftime(s2, "W/E %#m/%#d/%y")
employee = Projectsummaryplannedhours.objects.order_by().values_list('employeename', flat=True).distinct().filter(businessunit='a').filter(billinggroup__startswith='PLS - Pip')
emp1 = Projectsummaryplannedhours.objects.values_list('displayval', 'employeename').filter(businessunit='a').filter(billinggroup__startswith='PLS - Pip').filter(displayval=sunday).annotate(plannedhours__sum=Sum('plannedhours'))
emp4 = Projectsummaryplannedhours.objects.filter(employeename__startswith='Linn').filter(Q(displayval=sunday2)|Q(displayval=sunday)).annotate(plannedhours__sum=Sum('plannedhours'))
emp2 = Projectsummaryplannedhours.objects.values_list('displayval', 'employeename').filter(businessunit='a').filter(billinggroup__startswith='PLS - Pip').filter(Q(displayval=sunday2)|Q(displayval=sunday)).annotate(plannedhours__sum=Sum('plannedhours'))
emp3_list = Projectsummaryplannedhours.objects.values_list('displayval', 'employeename').filter(businessunit='a').filter(billinggroup__startswith='PLS - Pip').filter(Q(displayval=sunday2)|Q(displayval=sunday)).annotate(plannedhours__sum=Sum('plannedhours'))
emp3 = map(lambda x: {'date': x[0], 'employee_name': x[1], 'planned_hours': x[2]}, emp3_list)
context = {'emp1': emp1, 'emp2': emp2, 'sunday2': sunday2, 'employee': employee, 'emp3': emp3, 'emp4': emp4}
return render(request,'department_hub_ple.html', context)
答案 0 :(得分:0)
您可以通过以下方式使用列表索引:
{{ x.0 }}
因此,您的模板可能看起来像(没有任何标记或样式):
{% for x in emp3 %}
{{ x.1 }}
{{ x.2 }}
{% endfor %}
请参阅Docs on Variables and Lookups
如果您正在寻找一种仅使用列表列表中的列表的方法,则可以进行链式查找。以下示例将给出第一个列表的第二个元素。
{{ x.0.1 }}
这可能会使您的模板难以阅读,因为在您的上下文中x.0.1
引用的内容并不十分清楚。您可以考虑更改结果的数据结构,并将视图返回的数据限制为模板将要使用的数据。
要使您的模板更具可读性,请使用map
函数将列表列表转换回字典列表:
emp3_list = [
["W/E 6/11/17", "Carter, Jon", 40.0],
["W/E 6/18/17", "Carter, Jon", 40.0],
["W/E 6/11/17", "Linn, Kenneth", 27.0],
["W/E 6/18/17", "Linn, Kenneth", 27.0],
["W/E 6/11/17", "Massey, Smiley", 55.0],
["W/E 6/18/17", "Massey, Smiley", 45.0]
]
emp3 = map(lambda x: {'date': x[0], 'employee_name': x[1], 'planned_hours': x[2]}, emp3_list)
您的模板将如下所示:
{% for x in emp3 %}
Employee: {{ x.employee_name }}
Date: {{ x.date }}
Planned Hours: {{ x.planned_hours }}
{% endfor %}