Django模板,如果项目的ID等于父循环名称,则遍历所有项目 [英] Django template, loop through items if their id is equal to parent loop name

查看:43
本文介绍了Django模板,如果项目的ID等于父循环名称,则遍历所有项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遍历不同的区域,然后显示属于该区域的项目

I'm trying to loop through different Zones and then show the items which are part of this zone

区域是一个模型,具有名称和ForeignKey.Planche是一个以Zone为ForeignKey的模型.

Zone is a model, has a name and a ForeignKey. Planche is a model which has Zone as ForeignKey.

我正在遍历区域以显示每个区域.在该循环中,我将循环所有Planches,并只显示那些以Zone为外键的Planches.

I'm looping through zones to display each zone. In that loop I'm looping all Planches and would like to display only the ones that have Zone as a ForeignKey.

class Zones(models.Model):
    name = models.CharField(max_length=30)
    genre = models.ForeignKey(ZoneTypes, on_delete=models.CASCADE)

    def __str__(self):
        return self.name

class Planche(models.Model):
    pzone = models.ForeignKey(Zones, on_delete=models.CASCADE)
    ref = models.CharField(max_length=5, default="1")
    length = models.IntegerField()
    width = models.IntegerField()
    orientation = models.CharField(max_length=30)

    def __str__(self):
        return self.ref

模板

<div>
     <h1><a href="/">My list of planches</a></h1>
</div>
{% for z in zones %}
    <div>
        <h2><a href="/zone/{{ z.name }}">Zone name: {{ z.name }}</a></h2>
        {% for p in planches %}
        {% if p.pzone == z.name }
        <h1><a href="planche/{{ planche.ref }}">Ref: {{ p.ref }}</a></h1>
        <p>Length: {{ p.length }} - Width: {{ p.width }}</p>
        <p>Orientation: {{ p.orientation }}
        {% endif %}
        {% endfor %}
    </div>
{% endfor %}

{%,如果p.pzone = z.name%}返回False,如果我只显示它们{{p.pzone}}和{{z.name}},它们都返回相同的字符串,但是我猜它们不是同一数据类型.我尝试将它们转换为{%with%}语句中的字符串,但我一直失败

{% if p.pzone = z.name %} returns False, They both return the same string if I just display them {{ p.pzone }} and {{ z.name }} but I guess they aren't the same data type. I tried converting them to strings in a {% with %} statement but I keep failing

推荐答案

我假设您要显示每个区域的所有切缝.您可以使用 ForeignKey 上的 related_name 来访问引用当前对象的项目.您没有在此处设置任何相关名称,因此它是默认名称: planche_set .

I'm assuming you want to display all the planches for each zone. You can use the related_name on the ForeignKey to access to items referencing the current object. You did not set any related name there, so it's the default one: planche_set.

<div>
     <h1><a href="/">My list of planches</a></h1>
</div>
{% for z in zones %}
    <div>
        <h2><a href="/zone/{{ z.name }}">Zone name: {{ z.name }}</a></h2>
        {% for p in z.planche_set.all %}
        <h1><a href="planche/{{ planche.ref }}">Ref: {{ p.ref }}</a></h1>
        <p>Length: {{ p.length }} - Width: {{ p.width }}</p>
        <p>Orientation: {{ p.orientation }}
        {% endfor %}
    </div>
{% endfor %}

请注意,除非您在该方法中添加 prefetch_related('planche'),否则该方法将执行N + 1个查询(一个用于选择区域,然后每个区域一个查询以检索每个区域的板).查看您选择区域的位置.

Be aware that method will execute N+1 queries (One to select your zones then one query per zone to retrieve the planches of each zone) unless you add prefetch_related('planche') in the view where your select your zones.

参考文献:

这篇关于Django模板,如果项目的ID等于父循环名称,则遍历所有项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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