简体   繁体   English

Django-在for循环中访问列表的子列表

[英]Django - Accessing sublist of list in for loop

I have a query that outputs data in the following format: 我有一个查询,它以以下格式输出数据:

[["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]]

My query: 我的查询:

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'))
)

In my template, I'm currently using a for loop, but it returns all items in the list, rather than just the first list of lists. 在我的模板中,我当前正在使用for循环,但是它返回列表中的所有项目,而不仅仅是列表的第一个列表。

{% for x in emp3 %}
{{x.employeename}}
{{x.plannedhours__sum}}

What I would like to do is iterate through the list and display Employee: Value for W/E 6/11, Value for W/E 6/18 in a horizontal form. 我想做的是遍历列表并以水平形式显示Employee:W / E 6/11的值,W / E 6/18的值。

Model: 模型:

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.

Updated Query: 更新的查询:

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)

Every query I have tried: 我尝试过的每个查询:

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)

You can use list indexes in the following way: 您可以通过以下方式使用列表索引:

{{ x.0 }}

So your template might look like (without any markup or styling): 因此,您的模板可能看起来像(没有任何标记或样式):

{% for x in emp3 %}
  {{ x.1 }}
  {{ x.2 }}
{% endfor %}

See Docs on Variables and Lookups 请参阅有关变量和查找的文档

EDIT 编辑

If you're looking for a way to use only the list in your list-of-lists, you can chain lookups. 如果您正在寻找一种仅使用列表中列表的方法,则可以链接查找。 The following example would give the second element of the first list. 以下示例将给出第一个列表的第二个元素。

{{ x.0.1 }}

This probably makes your template harder to read, since it's not very clear what x.0.1 refers to in your context. 这可能会使您的模板更难阅读,因为不清楚x.0.1在您的上下文x.0.1是什么。 You might consider changing the data structure of the results, and limiting the data your view returns to just the data your template is going to use. 您可能会考虑更改结果的数据结构,并将视图返回的数据限制为仅模板将要使用的数据。

EDIT 2 编辑2

To make your template more readable, use a map function convert your list-of-lists back to a list-of-dicts: 为了使您的模板更具可读性,请使用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)

Your template would then look like this: 您的模板将如下所示:

{% for x in emp3 %}
    Employee: {{ x.employee_name }}
    Date: {{ x.date }}
    Planned Hours: {{ x.planned_hours }}
{% endfor %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM