【问题标题】:Django template, loop through items if their id is equal to parent loop nameDjango模板,如果它们的id等于父循环名称,则循环遍历项目
【发布时间】:2019-06-20 14:24:13
【问题描述】:

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

Zone 是一个模型,有一个名称和一个 ForeignKey。 Planche 是一个具有 Zone 作为 ForeignKey 的模型。

我正在遍历区域以显示每个区域。在该循环中,我正在循环所有 Planches,并且只想显示将 Zone 作为 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 %}

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

【问题讨论】:

    标签: python django


    【解决方案1】:

    我假设您想显示每个区域的所有平面。您可以使用ForeignKey 上的related_name 来访问引用当前对象的项目。您没有在此处设置任何相关名称,因此它是默认名称: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 %}
    

    请注意,该方法将执行 N+1 个查询(一个选择您的区域,然后每个区域一个查询以检索每个区域的平面图),除非您在选择您的 zones 的视图中添加 prefetch_related('planche')。

    参考资料:

    【讨论】:

      【解决方案2】:

      如果你想为每个区域显示 Planches,你可以这样写第二个循环:

      <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 }}
              {% endif %}
              {% endfor %}
          </div>
      {% endfor %}
      

      这是另一个帖子的示例:Django foreign key relation in template

      【讨论】:

      • 反向关系是FOO_set,其中 FOO 是模型名称,而不是字段名称,参见 Django 文档。
      • 非常感谢。我实际上是在学校时读到的,但我回家时忘记了
      猜你喜欢
      • 2019-06-21
      • 2023-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-13
      • 2018-06-14
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多