listing objects from ManyToManyField

Posted by Noam Smadja on Stack Overflow See other posts from Stack Overflow or by Noam Smadja
Published on 2010-05-03T16:23:42Z Indexed on 2010/05/03 16:28 UTC
Read the original article Hit count: 210

i am trying to print a list of all the Conferences and for each conference, print its 3 Speakers.

in my template i have:

{% if conferences %}
        <ul>
        {% for conference in conferences %}
                <li>{{ conference.date }}</li>
                {% for speakers in conference.speakers %}
                        <li>{{ conference.speakers }}</li>
                {% endfor %}
        {% endfor %}
        </ul>
{% else %}
<p>No Conferences</p>
{% endif %}

in my views.py file i have:

from django.shortcuts import render_to_response
from youthconf.conference.models import Conference

def manageconf(request):
        conferences = Conference.objects.all().order_by('-date')[:5]
        return render_to_response('conference/manageconf.html', {'conferences': conferences})

there is a model named conference. which has a class named Conferences with a ManyToManyField named speakers

i get the error: Caught an exception while rendering: 'ManyRelatedManager' object is not iterable with this line: {% for speakers in conference.speakers %}

© Stack Overflow or respective owner

Related posts about django-templates

Related posts about many-to-many