开发者

Django Nested Relations

开发者 https://www.devze.com 2022-12-15 18:15 出处:网络
I\'m not sure if I just can\'t concentrate or what, but I feel like this should be easy to do. I have 2 models, one that references the other as a simple foreign key relation (one-to-many) now in the

I'm not sure if I just can't concentrate or what, but I feel like this should be easy to do. I have 2 models, one that references the other as a simple foreign key relation (one-to-many) now in the template I want to display this re开发者_开发技巧lation as a nested unordered lists.


Not quite sure what you mean, but perhaps:

<ul>
    {% for foo in foo_list %}
    <li>
        {{ foo }}
        <ul>
            {% for bar in foo.bar_set.all %}
            <li>{{ bar }}</li>
            {% endfor %}
        </ul>
    </li>
    {% endfor %}
<ul>

Obviously, foo_list must be in the context, and bar has a foreign key relation to foo with no related_name.

0

精彩评论

暂无评论...
验证码 换一张
取 消