http://www.itblah.com/django-error-manyrelatedmanager-object-iterable/
while trying to iterate through a list of objects in a django template, i came across this error: “caught an exception while rendering: ‘manyrelatedmanager’ object is not iterable”
also of note is in the variables “<django.db.models.fields.related.manyrelatedmanager object at 0x9876545>” is returned rather than the value of that object.
to clarify, i’m trying to print a list of objects “items” associated with a model “things” through a manytomany relationship.
you may have guest, but i’ve changed the actual model names to help protect my project.
class thing(models.model):
# comment
def __unicode__(self): # python 3: def __str__(self):
return self.name
name = models.charfield(max_length=32)
desc = models.charfield(max_length=254)
img = models.charfield(max_length=32)
items = models.manytomanyfield(measurement)
category = models.manytomanyfield(category)
if “my_things” exists, we’re going to create a list of all the objects it contains.
if the “my_things” object contains any “items”, these will be listed in a nested list.
well, thats the plan. in red i’ve highlighted the reasons it fails.
…
{% if my_things %}
<ul>
{% for thing in my_things %}
<li>{{ thing }}</li>
{% for item in things.items %}
<li>{{ thing.item }}</li>
{% endfor %}
</ul>
{% else %}
how to fix “‘manyrelatedmanager’ object is not iterable” error:
note the changes in red
{% for item in things.items.all %}
<li>{{ item }}</li>