django: related_name of self related ForeignKey 字段不工作 |在模板中获得自我引用的相反方向
django: related_name of self related ForeignKey field not working | get opposite direction of self reference in template
嘿! :)
我有一个模型可以创建单一机构 (Institution
),并且可以通过 parent_institution
(自己)将其连接到上级机构。
所以我有机构 A,它是 b、c、d 的父机构(所有机构本身都有自己的详细信息视图。)在 b 的详细视图中,我有 'parent institution' 部分,结果是 A,包括 link 到 A.
的详细视图
<p><b>Parent institution: </b>
{% if institution.parent_institution %}
<a href="{% url 'stakeholders:institution_detail' institution.parent_institution.id %}">
{{institution.parent_institution}}
</a>
{% endif %}
</p>
在此 link 之后,我进入了 A 的详细视图,其中我想要包含子机构的部分。应该列出 b, c, d。
我在 parent_institution
中添加了相关名称
class Institution(models.Model):
name = models.CharField(
verbose_name=_("Name of the institution"),
max_length=200,
)
parent_institution = models.ForeignKey(
"self",
verbose_name=_("Parent institution"),
on_delete=models.SET_NULL,
blank=True,
null=True,
help_text=_("if applicable"),
related_name="child",
)
通常我可以通过这个 related_name.
在相反的方向跟随外键
<p><b>Child institution: </b>
{{institution.child.name}}
</p>
但在这种情况下,这不起作用并给了我 'None'。为此,我尝试了:
{% if institution.id == institution.all.parent_institution.id %}
{{institution.name}}
{% endif %}
{% if institution.all.id == institution.parent_institution.id %}
{{institution.name}}
{% endif %}
{% for child in institutions.all %}
{% if child.id == institution.parent_institution.id %}
{{institution.name}}
{% endif %}
{% endfor %}
# views.py
class InstitutionDetail(DetailView):
model = Institution
def get(self, request, *args, **kwargs):
institutions_child = Institution.objects.filter(parent_institution__isnull=True).prefetch_related('parent_institution_set')
institutions = get_object_or_404(Institution, pk=kwargs['pk'])
context = {'institutions_child': institutions_child, 'institutions': institutions}
return render(request, 'stakeholders/institution_detail.html', context)
{% for child_node in institutions.parent_institution_set.all %}
{{child_node.name}}
{% endfor %}
我得到 None 或当前机构名称 (A)。
有谁知道如何实现在详细视图中显示所有子机构的目标?
感谢任何帮助! :)
相反方向的外键returns一个查询集,而不是模型实例。
<p><b>Child institution: </b></p>
<ul>
{% for child in institutions.child.all %}
<li>{{ child.name }}</li>
{% endfor %}
</ul>
嘿! :)
我有一个模型可以创建单一机构 (Institution
),并且可以通过 parent_institution
(自己)将其连接到上级机构。
所以我有机构 A,它是 b、c、d 的父机构(所有机构本身都有自己的详细信息视图。)在 b 的详细视图中,我有 'parent institution' 部分,结果是 A,包括 link 到 A.
的详细视图<p><b>Parent institution: </b>
{% if institution.parent_institution %}
<a href="{% url 'stakeholders:institution_detail' institution.parent_institution.id %}">
{{institution.parent_institution}}
</a>
{% endif %}
</p>
在此 link 之后,我进入了 A 的详细视图,其中我想要包含子机构的部分。应该列出 b, c, d。
我在 parent_institution
class Institution(models.Model):
name = models.CharField(
verbose_name=_("Name of the institution"),
max_length=200,
)
parent_institution = models.ForeignKey(
"self",
verbose_name=_("Parent institution"),
on_delete=models.SET_NULL,
blank=True,
null=True,
help_text=_("if applicable"),
related_name="child",
)
通常我可以通过这个 related_name.
在相反的方向跟随外键<p><b>Child institution: </b>
{{institution.child.name}}
</p>
但在这种情况下,这不起作用并给了我 'None'。为此,我尝试了:
{% if institution.id == institution.all.parent_institution.id %}
{{institution.name}}
{% endif %}
{% if institution.all.id == institution.parent_institution.id %}
{{institution.name}}
{% endif %}
{% for child in institutions.all %}
{% if child.id == institution.parent_institution.id %}
{{institution.name}}
{% endif %}
{% endfor %}
# views.py
class InstitutionDetail(DetailView):
model = Institution
def get(self, request, *args, **kwargs):
institutions_child = Institution.objects.filter(parent_institution__isnull=True).prefetch_related('parent_institution_set')
institutions = get_object_or_404(Institution, pk=kwargs['pk'])
context = {'institutions_child': institutions_child, 'institutions': institutions}
return render(request, 'stakeholders/institution_detail.html', context)
{% for child_node in institutions.parent_institution_set.all %}
{{child_node.name}}
{% endfor %}
我得到 None 或当前机构名称 (A)。
有谁知道如何实现在详细视图中显示所有子机构的目标?
感谢任何帮助! :)
相反方向的外键returns一个查询集,而不是模型实例。
<p><b>Child institution: </b></p>
<ul>
{% for child in institutions.child.all %}
<li>{{ child.name }}</li>
{% endfor %}
</ul>