Show a django relationship in a template
Posted
by
kevin_82
on Stack Overflow
See other posts from Stack Overflow
or by kevin_82
Published on 2011-05-04T02:09:09Z
Indexed on
2012/06/21
15:16 UTC
Read the original article
Hit count: 191
django
I have a django model as follows:
class Person(models.Model):
name = models.CharField(max_length=255)
class Relationship(models.Model):
parent = models.ForeignKey(Person)
child = models.ForeignKey(Person)
description = models.TextField(blank=True)
In my view, I pass a certain person, and the relationships in which he/she is parent:
person = Person.objects.filter(name ='some name')
descendant_relationships = Relationship.objects.filter(parent = person)
An I want to show this person's descendants in a list in a template:
<ul>
{% for item in descendant_relationships%}
<li> {{item.child.name}} - {{item.description}} </li>
{% endfor %}
</ul>
But this template code will not show the children of children (i.e. grandchildren, great-grandchildren etc.). How can I get these lower level descendants to show up? I imagine recursion is necessary somewhere, but where?
© Stack Overflow or respective owner