【问题标题】:how to get family tree using Django template and from ManyToMany field?如何使用 Django 模板和从 ManyToMany 字段获取家谱?
【发布时间】:2015-06-01 13:23:27
【问题描述】:

我需要家谱,我这里没有使用任何库(你能建议一下吗)。首先你可以先看看我的模型,这样你就会知道这个想法。

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)

我的视图函数看起来像

def explore_home(request):
    old_date = FamilyMember.objects.filter(familyname__name = family_name).aggregate(Min('birth_date'))
    member_obj_all = FamilyMember.objects.get(familyname__name = family_name, birth_date=old_date['birth_date__min'])

我正在使用@987654321@ 构建家谱。

在我的 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>

上面的代码正常工作,输出就像......

但是,如果我有 n 个孩子,然后是孩子的孩子,以及如何再次自动化该过程。? (在模板中?)。看到我只是有这个想法,如果你有任何想法,请在这里告诉我。

【问题讨论】:

  • 你可以使用递归函数来不断调用自己(模板的“我们再来一次”部分),显然你的视图中应该有递归函数,你应该返回一个字典此处解释的所有嵌套级别stackoverflow.com/questions/6803599/…

标签: python django


【解决方案1】:

试试这样的(未测试)

//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>

【讨论】:

  • 超出最大递归深度
  • 尝试将{% if child.children.all %} 替换为count - {% if child.children.count %}。但是你应该明白,有一个模板呈现一个孩子,并且对于孩子的每个孩子,模板都包含自己。
  • 你能检查你的数据吗?有没有机会和他的父母一起生孩子,例如FamilyMember A 有孩子[FamilyMember B, FamilyMember C]FamilyMember B 有孩子[FamilyMember D, FamilyMember A] 这会导致无限递归。
  • 谢谢哥们...我的一些数据处理不当..我修复了它现在神奇地工作..你拯救了我的一天..谢谢..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 2013-05-12
  • 2022-07-17
  • 2021-12-29
  • 2014-02-04
  • 2021-02-22
相关资源
最近更新 更多