我正在嘗試打印所有會議的列表,併爲每個會議打印其3個揚聲器。列出ManyToManyField中的對象
在我的模板,我有:
{% 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 %}
在我views.py文件我有:
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})
有一個名爲會議模型。其中有一個名爲類會議與ManyToManyField命名揚聲器
我得到的錯誤:
Caught an exception while rendering: 'ManyRelatedManager' object is not iterable
這一行:{% for speakers in conference.speakers %}
非常感謝你:)是不是隻要我想! – 2010-05-03 17:02:19
如果在我看來,我希望將ManyRelatedManager轉換爲傳遞查詢的列表? – EduardoMaia 2017-10-18 17:43:37