djangodjango-templates

Get related objects attributes


I'm trying to make some kind of menu builder, where one model can be self-assigned with ForeignKey.

    class MainMenuItem(models.Model):
        class Meta:
            db_table = "menu_item"
            verbose_name = 'Раздел'
            verbose_name_plural = 'Разделы'     
        title = models.CharField(max_length = 50)
        parentFolder = models.ForeignKey('self', unique=False, related_name="childrenFolders", blank = True, null = True)

        def __unicode__(self):
            return self.title

        def __str__(self):
            return self.title

And now I'm stuck with a really simple thing...) But anyway... I can't get items, which assigned to the another item. I wrote that in my template:

    {% for item in menuItems %}

        <li>{{item.childrenFolders.all}}</li>

    {% endfor %}

and got an array [<MainMenuItem: item1>, <MainMenuItem: item2>, <MainMenuItem: item3>]

But what I need to write after childrenFolders.all... to get a title field? Cause when I write something like childrenFolders.all.item.title, I got nothing...


Solution

  • Try:

    {% for item in menuItems %}
        {% for childItem in item.childrenFolders.all %}
            <li>{{childItem.title}}</li>
        {% endfor %}
    {% endfor %}