'RelatedManager' object is not iterable

1,964 views
Skip to first unread message

british.assassin

unread,
Aug 4, 2009, 2:16:59 PM8/4/09
to Django users
Hi,

In the views.py for my app I have the following:

def index(request):
categories = models.Category.objects.all()
return render_to_response("forum/index.html",
{'categories':categories})

And in the template file I am trying to iterate through these via
doing:

{% for category in categories %}

But every time I try this I get an exception saying: 'RelatedManager'
object is not iterable.

I was under the impression that the output from obkects.all() was
iterable?

I would appreciate any help anyone can give.

Thanks

Alex Gaynor

unread,
Aug 4, 2009, 2:29:25 PM8/4/09
to django...@googlegroups.com
It is, you've done something, either in your template or in your
models that's somehow causing the issue. If you could show us those
we'd have a better idea of what the issue is.

Alex

--
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me

british.assassin

unread,
Aug 4, 2009, 3:15:06 PM8/4/09
to Django users
Yeah sure.

Models.py:

from django.db import models
from django.contrib.auth.models import User

class Category(models.Model):
name = models.CharField(max_length=100)

class Meta:
verbose_name_plural = 'categories'

def __unicode__(self):
return self.name

class Forum(models.Model):
category = models.ForeignKey(Category,related_name='forums')
name = models.CharField(max_length=100)

def __unicode__(self):
return self.name

class Topic(models.Model):
forum = models.ForeignKey(Forum,related_name='topics')
subject = models.CharField(max_length=100)
views = models.IntegerField()

def __unicode__(self):
return self.subject()

class Post(models.Model):
topic = models.ForeignKey(Topic,related_name='posts')
created = models.DateTimeField(auto_now_add=True)
body = models.TextField()
author = models.ForeignKey(User,related_name='posts')

def __unicode__(self):
return self.subject

Views.py:

from django.shortcuts import render_to_response
import models

def index(request):
categories = models.Category.objects.all()
return render_to_response("forum/index.html",
{'categories':categories})

Index.html:

{% extends "base.html" %}

{% block title %}EvoWebs{% endblock %}

{% block body %}
{% if categories %}
{% for category in categories %}
<table border="1">
<tr><td>{{category.name}}</td></tr>
{% for forum in category.forums %}
<tr><td>{{forum.name}}</td></tr>
{% endfor %}
</table>
{% endfor %}
{% else %}
<p>This forum has no categories</p>
{% endif %}
{% endblock %}

Base.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en-GB" xml:lang="en-GB" xmlns="http://www.w3.org/1999/
xhtml">
<head>
<title>{% block title %}{% endblock %}</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>

Daniel Roseman

unread,
Aug 4, 2009, 3:56:03 PM8/4/09
to Django users
On Aug 4, 8:15 pm, "british.assassin" <british.assas...@gmail.com>
wrote:
>
> {% extends "base.html" %}
>
> {% block title %}EvoWebs{% endblock %}
>
> {% block body %}
> {% if categories %}
> {% for category in categories %}
> <table border="1">
> <tr><td>{{category.name}}</td></tr>
> {% for forum in category.forums %}
> <tr><td>{{forum.name}}</td></tr>
> {% endfor %}
> </table>
> {% endfor %}
> {% else %}
> <p>This forum has no categories</p>
> {% endif %}
> {% endblock %}
>

It seems more likely that the error is coming from this line:
{% for forum in category.forums %}

It should be
{% for forum in category.forums.all %}
--
DR.

british.assassin

unread,
Aug 4, 2009, 4:02:14 PM8/4/09
to Django users
Yeah, that did it, thanks. I didn't even notice that, lol

Thanks again.
Reply all
Reply to author
Forward
0 new messages