tree structure of parent child relation in django templates

前端 未结 4 2222
借酒劲吻你
借酒劲吻你 2021-01-31 12:40

how do i implement the tree structure in django templates with out using django-mptt.

i have model.

class Person(TimeStampedModel):
    name  = models.Ch         


        
4条回答
  •  忘掉有多难
    2021-01-31 13:40

    It's very simple

    All you have to do in your view is get all objects:

    people = Person.objects.all()
    

    Then in your template :

    {% for person in people %}
      
  • - {{person.name}}
  • {% for child in person.children.all %}
      * {{child.nom}}
    {% endfor %}
  • {% endfor %}

提交回复
热议问题