ManyRelatedManager object is not iterable

2,632 views
Skip to first unread message

Todd O'Bryan

unread,
May 29, 2007, 9:38:17 PM5/29/07
to django...@googlegroups.com
With the latest from SVN, I'm getting an error in a template, but I
can't figure out why...

def obligation_list(request, org_name):
org = Organization.objects.get(slug=org_name)
oblig_list = org.obligation_set.all()
return render_to_response('obligationList.html',
{'oblig_list': oblig_list, 'organization':
org},
RequestContext(request))

Here's the (relevant part of the) model:

class Obligation(models.Model):
organization = models.ForeignKey(Organization, editable=False)

And the (relevant part of the) error I get in the browser:

Template error
In
template /home/tobryan1/workspace/dmi/src/dmi/orgs/templates/obligationList.html, error at line 18


Caught an exception while rendering: 'ManyRelatedManager' object is not
iterable

18 {% for obligation in oblig_list %}

Any ideas what's going on?

Thanks,
Todd


Russell Keith-Magee

unread,
May 29, 2007, 9:50:11 PM5/29/07
to django...@googlegroups.com
On 5/30/07, Todd O'Bryan <toddo...@mac.com> wrote:
>
> Caught an exception while rendering: 'ManyRelatedManager' object is not
> iterable
>
> 18 {% for obligation in oblig_list %}
>
> Any ideas what's going on?

The Manager isn't iterable, but the query sets it produces are.

You're looking for:

{% for obligation in oblig_list.all %}

Yours,
Russ Magee %-)

Todd O'Bryan

unread,
May 29, 2007, 10:25:33 PM5/29/07
to django...@googlegroups.com
But didn't I already do that in the view? Or am I not allowed to do
that?

def obligation_list(request, org_name):
org = Organization.objects.get(slug=org_name)

--> oblig_list = org.obligation_set.all()


return render_to_response('obligationList.html',
{'oblig_list': oblig_list, 'organization':
org},
RequestContext(request))

Todd

Mohamed Hussein

unread,
May 29, 2007, 11:25:23 PM5/29/07
to django...@googlegroups.com
I have been trying to unsubscribe from this list for some time and it is
not working.
Can the list maintainer please remove me from the list?

--
Mohamed Hussein
http://www.unixgarage.com


Todd O'Bryan

unread,
May 30, 2007, 7:43:52 AM5/30/07
to django...@googlegroups.com
On Tue, 2007-05-29 at 21:38 -0400, Todd O'Bryan wrote:
> Template error
> In
> template /home/tobryan1/workspace/dmi/src/dmi/orgs/templates/obligationList.html, error at line 18
>
>
> Caught an exception while rendering: 'ManyRelatedManager' object is not
> iterable
>
> 18 {% for obligation in oblig_list %}
>
OK. I figured out the problem. Russ was right that I had forgotten
a .all--the problem is that it wasn't here. There was another for-loop
nested in this one, and that was where the problem was. It's a mystery
to me why the error was reported with this line instead of the line
where it actually occurred. Is that maybe a bug?

Todd

Russell Keith-Magee

unread,
May 30, 2007, 9:42:50 AM5/30/07
to django...@googlegroups.com
On 5/30/07, Todd O'Bryan <toddo...@mac.com> wrote:
>

Well, yes, if only because the error message made you look in the
wrong place. If you can provide a specific (cut down) example where
the template parser produces the wrong line number, it may be possible
to improve the error message.

Yours,
Russ Magee %-)

Reply all
Reply to author
Forward
0 new messages