[Answered ]-How to display objects grouped by foreign key

2👍

I think you are looking for regroup

food = Food.objects.all()

{% regroup food by food_type as food_list %}

<ul>
{% for food_type in food_list %}
    <li>{{ food_type.grouper }}
    <ul>
        {% for item in food_type.list %}
        <li>{{ item }}</li>
        {% endfor %}
    </ul>
    </li>
{% endfor %}
</ul>
👤dting

0👍

Simple:

Food.objects.order_by('food_type__name')

Leave a comment