[Answer]-Django getting list of records (with FK) related to object -not rendering template correctly

1👍

✅

You are doing

{% for v in owners.petname_set.all %}

where you should be doing

{% for v in i.petname_set.all %}

owners is the queryset, but you need to get the petname_set for the individual object in the queryset which is i in this case.

Also, I would recommend a condition check if i.petname_set exists. If not, do not show the has pets: text.

{% for i in owners %}
    {{ i.owner }} 
    {% if i.petname_set.count %} has pets:<br />
        {% for v in i.petname_set.all %} //this doesn't seem to be executing
         - {{ v.pet_name }}<br />
        {% endfor %}
    {% endif %}
{% endfor %}
👤karthikr

Leave a comment