DJANGO问题--Error: ‘ManyRelatedManager’ object is not iterable
http://www.itblah.com/django-error-manyrelatedmanager-object-iterable/
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 %}
…
DJANGO问题--Error: ‘ManyRelatedManager’ object is not iterable的更多相关文章
- 'ManyRelatedManager' object is not iterable
先看下面的代码: class Worker(models.Model): departments = moels.ManyToManyField(Department, verbose_name=u& ...
- Django报:builtin_function_or_method' object is not iterable
def detail(request,hero_id): hero=models.HeroInfo.objects.get(id=hero_id) return render_to_response( ...
- 怎样处理“error C2220: warning treated as error - no object file generated”错误
最近用VS2010 编译ceflib开源库是出现"怎样处理"error C2220: warning treated as error - no object file gener ...
- HBase Error: connection object not serializable
HBase Error: connection object not serializable 想在spark driver程序中连接HBase数据库,并将数据插入到HBase,但是在spark集群提 ...
- unexpected error ConnectionError object has no attribute
unexpected error ConnectionError object has no attribute
- 问题-Error creating object. Please verify that the Microsoft Data Access Components 2.1(or later) have been properly installed.
问题现象:软件在启动时报如下错误信息:Exception Exception in module zhujiangguanjia.exe at 001da37f. Error creating obj ...
- error C2220: warning treated as error - no 'object' file generated解决方法
error C2220: warning treated as error - no 'object' file generated 警讯视为错误 - 生成的对象文件 / WX告诉编译器将所有警告视为 ...
- 安装Django时报错'module' object has no attribute 'lru_cache'
使用pip方法安装Django时报错'module' object has no attribute 'lru_cache' 解决办法如下 命令行输入命令sudo pip install Django ...
- 'NoneType' object is not iterable
"TypeError: 'NoneType' object is not iterable" 一般是返回值为None同时赋值给了多个变量
随机推荐
- 在线生成ICO图标、站标
网上一搜有很多,找了两个比较好用的,分别是http://ico.storyren.com/和http://www.ico.la/,前面的那个好像更好点.上传png.jpg.或gif格式的图片,按自己需 ...
- Python单元测试——深入理解unittest (转)
单元测试的重要性就不多说了,可恶的是Python中 有太多的单元测试框架和工具,什么unittest, testtools, subunit, coverage, testrepository, no ...
- 腾讯微博OAuth2.0 .NET4.0 SDK 发布以及网站腾讯微博登陆示例代码(原创)
1.使用简单方便,包含详细注释: 2.暂时只支持xml格式字符串的转换,建议接口使用xml参数:3.QweiboSDK.Controllers命名空间下已包含所有API接口:4.只需调用到Qweibo ...
- 重拾C,一天一点点_6
break与continuecontinue只能用于循环语句goto最常见的用法是终止程序在某些深度嵌套的结构中的处理过程,例如一次跳出两层或多层循环.break只能从最内层循环退出到上一级的循环. ...
- getchar(),gets(),scanf()的差异比较
scanf( )函数和gets( )函数都可用于输入字符串,但在功能上有区别.若想从键盘上输入字符串"hi hello",则应该使用gets()函数. gets可以接收空格:而sc ...
- RDD的转换操作---RDD转换过程
1) union(otherRDD)RDD-->UnionRDD2) groupByKey(numPartitions)RDD-->ShuffledRDD-->MapPartitio ...
- 在Linux下写一个简单的驱动程序
本文首先描述了一个可以实际测试运行的驱动实例,然后由此去讨论Linux下驱动模板的要素,以及Linux上应用程序到驱动的执行过程.相信这样由浅入深.由具体实例到抽象理论的描述更容易初学者入手Linux ...
- 使用ab测试工具 进行并发测试
ab.exe -n1000 -c100 http://localhost:8067/api/todo/555e95feb301baa678141148 http://www.cnblogs.com/y ...
- SQL日语词汇
データベース 数据库 DATABASE インスタンス (数据库)实例 INSTANCE ユーザー 用戶 USER ログイン・ログアウト ログオン・ログオフ 登录 LOGIN/LOGOUT LOGNO/ ...
- 问题 K: 【USACO2012Feb】植草 {Bronze题2}
按着矩形周长的思路,到当前边的时候,前一层的覆盖数乘以高度加入 ans 就行,然而真正的算法可能并不是这个..只能想到这个了 ; type node=record l,r,mid,sum,delta: ...