DJANGO问题--Error: ‘ManyRelatedManager’ object is not iterable
2016-02-23 11:14
1896 查看
http://www.itblah.com/django-error-manyrelatedmanager-object-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.
# 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 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>
<ul>
{% for item in things.items %}
<li>{{ thing.item }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
{% else %}
…
How to fix “‘ManyRelatedManager’ object is not iterable” error:
Note the changes in RED
…
{% if my_things %}
<ul>
{% for thing in my_things %}
<li>{{ thing }}</li>
<ul>
{% for item in things.items.all %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
{% else %}
…
Django: Error: ‘ManyRelatedManager’ object is not 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.
Lets take a look at the model:
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)
Lets take a look at the template:
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>
<ul>
{% for item in things.items %}
<li>{{ thing.item }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
{% else %}
…
How to fix “‘ManyRelatedManager’ object is not iterable” error:
Note the changes in RED
…
{% if my_things %}
<ul>
{% for thing in my_things %}
<li>{{ thing }}</li>
<ul>
{% for item in things.items.all %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endfor %}
</ul>
{% else %}
…
相关文章推荐
- Attempt to invoke interface method 'boolean android.database.Cursor.requery()' on a null object refe
- iOS Developer Libray (中文版)-- About Objective-C
- JavaScript的Function与Object浅析
- Object-C中nil、Nil、NULL与NSNull的一些理解
- 【Objective-C】07-自定义构造方法和description方法
- 【Objective-C】08-self关键字
- objective-C : 工厂方法
- JavaScript Object Notation(json)
- instancetype、id、NSObject的区别
- Object
- Salient Object Detection via Structured Matrix Decomposition
- 关于Object Bank的几篇文章解析
- iOS开发之 Objective-C语言基础 :代码块
- GitHub 上排名前 100 的 Objective-C 项目简介
- Objective-C中的Block
- 保存自定义对象 archivedDataWithRootObject unarchiveObjectWithData
- QT 控件和ID绑定 QObjectUserData
- magent 启动时error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: N
- Objective C运行时总结
- Java对象序列化ObjectOutputStream和ObjectInputStream示例