Django-帮助在嵌套的for循环中显示内容

时间:2018-09-04 14:53:07

标签: django django-models django-templates django-views

我有一个请购单,其中可能有多行。因此,为了实现这一点,我在RequisitionLine模型的两个字段上包括一个unique_together约束,其中一个字段是Requisition的FK。因此,很自然地拉出请购单的所有行,我将查询RequisitionLine表,其中FK = Requisition模型的ID,然后遍历所有序列以获取所有行。

我的目标是在模板的标题下显示标题编号以及该申请的行,但是我很难做到这一点。我试图遍历queryset,但是我在下面发布的代码是我最近的尝试,尝试将通行证列表传递到模板,该模板也不起作用。现在,每个标题显示所有行。任何帮助将不胜感激。

Models.py

class Requisition(models.Model):
    username = models.ForeignKey(
        'users.CustomUser', on_delete=models.CASCADE, related_name='req_user')

    signature = models.CharField(max_length=10, blank=True, null=True)



class RequisitionLine(models.Model):

    parent_req = models.ForeignKey('Requisition', on_delete=models.CASCADE, related_name='par_req_line' )
    sequence = models.PositiveIntegerField()
    item_code = models.ForeignKey(
        'items.ItemMaster', on_delete=models.CASCADE, related_name='req_item', blank=True, null=True)
    description = models.CharField(max_length=50, blank=True)
    extra_information = models.TextField(blank=True)
    quantity = models.PositiveIntegerField(blank=True, default=0,null=True)
    price = models.DecimalField(max_digits=19, decimal_places=2, blank=True, default=0.00,null=True)
    purchase_order = models.CharField(max_length=9, blank=True,null=True)
    po_line = models.PositiveSmallIntegerField(blank=True,null=True)
    req_delivery_date = models.DateField(blank=True,null=True)
    act_delivar_date = models.DateField(blank=True, null=True)
    status = models.ForeignKey('RequisitionStatus', related_name='req_status', on_delete=models.CASCADE, blank=True, null=True)
    assistance = models.ForeignKey('users.UserRoles', related_name='req_assist', blank=True, null=True, on_delete=models.CASCADE, limit_choices_to= ~Q(role='Requestor'))
    catagory = models.ForeignKey('items.ItemCatagory', on_delete=models.CASCADE, related_name='line_cat',  blank=True, null=True)
    notes = models.TextField(blank=True)

    class Meta:
        unique_together = ('parent_req','sequence')

Views.py

def pending_action(request):
user = CustomUser.objects.get(username=request.user)
user_req_headers = Requisition.objects.filter(username=user)
complete_status = RequisitionStatus.objects.get(status='Completed')
req_line_list = []

for req_header in user_req_headers:
    req_lines = RequisitionLine.objects.filter(Q(parent_req = req_header) & ~Q(status=complete_status))
    req_line_list.append(req_lines)

return render(request, 'req/pending_action.html', {'user_req_headers':user_req_headers,'req_line_list':req_line_list}) 

pending_action.html

{% for header in user_req_headers %}    
    <h3>{{header}}</h3>
    {% for req_line in req_line_list %}
    {% for req in req_line %}    
    <table>
        {% if forloop.first  %}
        <tr>
            <th>description</th>
            <th>catagory</th>
        </tr>
        {% endif %}
        <tr>
            <!-- FOR LOOP HERE TO ITERATE OF LIST OF QUERYSETS-->

            <td>{{ req }}</td>

        </tr>
    </table>
    {% endfor %}
    {% endfor %}
{% endfor %}

1 个答案:

答案 0 :(得分:0)

我通过创建标头的查询集并使用反向关系来捕获与每个标头关联的行来解决此问题

<table class="table">
        {% for req_header in req_header_list %}
            <tr>

                <ul>{{ req_header }}
                {% for line in req_header.par_req_line.all %}
                    <li>{{line}}</li>
                {% endfor %}
                </ul>
            </tr>

        {% endfor %}

    </table>