嵌套重组-Django [英] Nested Regroups - Django

查看:86
本文介绍了嵌套重组-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,其中包含以下字段:日期,员工和计划工时。每个员工在不同的日期都有不同的计划时间。

I have a model with the following fields: "Date", "Employee", and "Planned Hours". Each employee has various planned hours for various dates.

我正在尝试构建模板,其中在正确的对应日期下,雇员在行中列出,其计划工时在列中列出。

I'm attempting to structure my template where employees are listed in rows and their planned hours are listed in columns under the correct corresponding date.

类似的东西

到目前为止,我的模板如下:

My template looks like this so far:

{% regroup emp3 by employee_name as emp9 %}
{% for employee_name in emp9 %}
<!--Job-->
<div class="table-row table-job-column employee-row"{{employee_name.grouper}}</div>

{% for x in employee_name.list %}

<div class="table-row table-fr-column">{{x.planned_hours}}</div>

{% endfor %}
{% endfor %}

我的观点:

def DesignHubR(request):
emp3_list = Projectsummaryplannedhours.objects.values_list('displayval', 'employeename').filter(businessunit='a').filter(billinggroup__startswith='PLS - Project').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 = {'sunday': sunday, 'sunday2': sunday2, 'emp3': emp3}
return render(request,'department_hub_ple.html', context)

当前结构的问题是将第一个计划时间放在员工旁边。如果该日期没有计划的时间,我需要能够插入一个空白列。我知道自己没有包含嵌套的重组,但是我尝试过的所有方法都无效。

My issue with the current structure is it places the first "planned hour" next to the employee. I need to be able to insert a blank column if there are no planned hours for that date. I realize I haven't included a nested regroup, but everything I've tried hasn't worked.

注意:我将列日期拉到重组的外部。

Note: I'm pulling my column dates in outside the regroup.

编辑以包含EMP3_LIST:

EDIT TO INCLUDE EMP3_LIST:

[{'date': 'W/E 6/18/17', 'planned_hours': Decimal('45.00000'), 'employee_name': 'Bob'}, {'date': 'W/E 6/25/17', 'planned_hours': Decimal('45.00000'), 'employee_name': 'Bob'}, {'date': 'W/E 6/18/17', 'planned_hours': Decimal('17.00000'), 'employee_name': 'Jack'}, {'date': 'W/E 6/25/17', 'planned_hours': Decimal('13.00000'), 'employee_name': 'Jack'}, {'date': 'W/E 6/25/17', 'planned_hours': Decimal('1.00000'), 'employee_name': 'Stan'}, {'date': 'W/E 6/18/17', 'planned_hours': Decimal('27.00000'), 'employee_name': 'Albert'}, {'date': 'W/E 6/25/17', 'planned_hours': Decimal('29.00000'), 'employee_name': 'Albert'}]

编辑2:

{% regroup emp3 by employee_name as emp9 %}
{% for employee_name in emp9 %}
<!--Job-->
<div class="table-row table-job-column employee-row">
{{employee_name.grouper}}</div>
{% regroup employee_name.list by date as date_list %}
{% for x in date_list %}
{% regroup x.list by planned_hours as plan_list %}
{% for y in plan_list %}

<div class="table-row table-fr-column">{{y.grouper}}</div>

{% endfor %}{% endfor %}


推荐答案

您是否尝试过重新组合?您还尝试过基于类的视图吗?它们对于快速生成代码非常有用。

Have you tried to regroup your regroup? Also have you tried ClassBased Views at all? They are really useful for quick code generation. Something like the following.

视图:

Class EmployeeTimeSheetView(ListView):
    model = Projectsummaryplannedhours
    template_name = "department_hub_ple.html"

    def get_queryset(self): 
       return Projectsummaryplannedhours.objects.all().order_by('-date')

模板:

{% regroup object_list by date|date:"m/d/Y" as date_list %}
    {% for date in date_list %}
     ###html code {{ date.grouper }}
    {% regroup date.list by employee as employee_list %}
        {% for employee in employee_list %}
            ###html code {{ employee.grouper }}
        {% endfor %}
     {% endfor %}

这将使您可以建立一个以日期为列标题的表,然后在下面的oder中列出其计划工时。可能需要进行一些调整才能显示所需的信息,而这可能只是您重新分组依据的字段。

This should allow you to build a table with dates as the column headers and then list the employees in oder below with their planned hours. It might require a little tweaking to get it to display the information you want and that might just comedown to which fields you regroup by.

我使用这种重新分组的方法相关的项目按月,然后按周。

I use this kind of regroup to group related items by Month then by Week.

这篇关于嵌套重组-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆