[Answered ]-How to make a dictionary of two sets of related objects in django?

2πŸ‘

βœ…

t.creator is already the User object so you can assign it as:

ops[t] = t.creator

And pythonic way of building dicts is the passing list of two-item tuples to the dict constructor:

ops = dict((t.title, t.creator) for t in topics)

But anyway you don’t need the dict of strings to iterate in the template. Pass the queryset as is:

def forum(request, forum_id):
    topics = Topic.objects.select_related('creator') \
                          .filter(forum=forum_id).order_by("-created")    
    return render(request, 'forum.html', {'topics': topics})

And the in the forum.html:

<ul>
{% for topic in topics %}
    <li>{{ topic.title }} by {{ topic.creator }}</li>
{% endfor %}
</ul>
πŸ‘€catavaran

Leave a comment