如何使用Django模板从manytomy字段获取家谱?

2024-03-28 19:39:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要家谱,我这里不使用任何图书馆(你能建议吗)。首先你可以先看看我的模型,这样你就会知道这个想法了。在

class FamilyMember(models.Model):
    familyname = models.ForeignKey(FamilyName)
    first_name = models.CharField(max_length=50)
    parents = models.ManyToManyField('self', null=True, blank=True, related_name='p', symmetrical=False)
    children = models.ManyToManyField('self', null=True, blank=True, related_name='c', symmetrical=False)

我的视图函数看起来像

^{pr2}$

我正在使用this code来构建家族树。在

在我的django模板中我确实喜欢

<div class="tree">
    <ul>
        <li>
            <a href="#">{{ family_name }}</a>
            <ul>
                <li>
                    <a href="#">{{ member_obj_all }}</a>
                    {% if member_obj_all.children.all %}

                    <!-- here we go again -->
                        <ul>
                            {% for child_obj in member_obj_all.children.all %}
                                <li>
                                    <a href="">{{ child_obj }}</a>

                                    {% if child_obj.children.all %}
                                    <!-- here we go againg -->
                                    <ul>
                                        {% for grand_child_obj in child_obj.children.all %}
                                            <li>
                                                <a href="">{{ grand_child_obj }}</a>
                                            </li>
                                        {% endfor %}
                                    </ul>
                                    {% endif %}

                                </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            </ul>
        </li>
    </ul>
</div>

好的上面的代码工作,输出就像。。。在

enter image description here

但是,如果我有n个孩子,然后是孩子的孩子,又如何使这个过程自动化呢。?(在模板中?)。我有这个想法,如果你有什么想法,请告诉我。在


Tags: nameselfchildtrueobjmodels孩子li
1条回答
网友
1楼 · 发布于 2024-03-28 19:39:11

试一下这样的方法(未经测试):

//child.html
<a href="#">{{child}}</a>
{% if child.children.count %}
    <ul>
        {% for child_obj in child.children.all %}
        <li>
            {% include 'child.html' with child=child_obj %}
        </li>
        {% endfor %}
    </ul>
{% endif %}


//tree.html
<div class="tree">
    <ul>
        <li>
            <a href="#">{{ family_name }}</a>
            <ul>
                <li>
                    <a href="#">{{ member_obj_all }}</a>
                    {% if member_obj_all.children.count %}
                        <ul>
                            {% for child_obj in member_obj_all.children.all %}
                            <li>
                                {% include 'child.html' with child=child_obj %}
                            </li>
                            {% endfor %}
                        </ul>
                    {% endif %}
                </li>
            </ul>
        </li>
    </ul>
</div>

相关问题 更多 >