我在Django中很新... 我的问题:如何通过listView在模板中获取“自我外键”名称? 感谢...
models.py
class Organization(Model.models):
name = models.CharField(max_length=100)
mother_organization = models.ForeignKey('self',
null=True,
blank=True,
related_name='mother'
)
views.py
class List(ListView):
model = Organization
context_object_name = "organizations"
template_name = "organizations.html"
organizations.html
{% for organization in organizations %}
<p>
{{ organization.reference }}
{{ organization.name }}
test 0 : OK !!
{{ organization.mother_organization.name|default:"-" }}
test 1 : FAIL
{{ organization.mother.name|default:"-" }}
test 2 : FAIL
{% for mother in organization.mother.all %}
{{ mother.name }}
{% empty %}
test 3 : FAIL
{% for mother in organization.mother_organization.all %}
{{ mother.name }}
{% empty %}
#
{% endfor %}
</p>
{% endfor %}
答案 0 :(得分:1)
您的字段名称为mother_organization
,因此您应在模板中使用相同的字段名称:
{{ organization.mother_organization.name|default:"-" }}