[Django]-How do I iterate ManyToMany field in Django template tag?

6👍

It’s because if you call b.category it returns only the relation object. To get its values (category objects) you have to add .all. Like this:

{% for b in books %}
<div>
    {{ b.title }}
    {% for cat in b.category.all %}
        {{cat}}
    {% endfor %}
</div>
{% endfor %}

By the way, I’ve also changed c.title to b.title, because I assume you want this book title, not something from global.

Leave a comment