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同时赋值给了多个变量
随机推荐
- 网络A、B、C类IP地址的区别
学网络之前得先要明白几个概念:(起初我也不怎么知道后来就慢慢会了) 1字节=8位,1位就是1个数字,所以1字节等于8个数字. 2的8次方,和二进制11111111转换为十进制并不一回事. 0-127是 ...
- Mentor PADS 9.5下载安装及破解指南
Pads,是一款用于设计.模拟电子线路及设计电路板的电脑软件,原由Innoveda公司开发,其后改名为PowerPCB,在2002年4月Innoveda被Mentor Graphics收购,近年再次改 ...
- CodeForces 478B 第八次比赛 B题
Description n participants of the competition were split into m teams in some manner so that each te ...
- c语言调试接口
http://blog.chinaunix.net/uid-10106787-id-2985587.html 在C语言程序设计中,常会出现各种各样的bug:段错误.参数异常等等.我们需要尽快定位错误, ...
- Oracle RAC中的一台机器重启以后无法接入集群
前天有个同事说有套AIX RAC的其中一台服务器重启了操作系统以后,集群资源CSSD的资源一直都在START的状态,检查日志输出有如下内容: [ CSSD][1286]clssnmv ...
- 【采集】php str_replace
<?php function my_str_replace($xmlHttp,$order='asc'){ if($order=='asc'){ return str_replace(array ...
- Entity Framework Power Tools安装和使用
Entity Framework Power Tools是一个由EntityFramework开发小组提供的工具,它可以从现有数据库生成Fluent款式的Code First代码. 大致来说,这个工具 ...
- css/js online online code editor/formator/debuger
http://cssdeck.com/labs http://jsfiddle.net/ http://fiddle.jshell.net/ support console http://plnkr ...
- VBS基础篇 - 变量
VBScript只有一种数据类型 —— Variant,它是根据上下文来判断是数字还是字符串.因为Variant是VBScript中唯一的数据类型,所以它也是VBScript中所有函数的返回值的数据类 ...
- android退出登陆后,清空之前所有的activity,进入登陆主界面
如题: android退出登陆后,清空之前所有的activity,进入登陆主界面 在退出登陆时只需要增加一个intent标志 Intent intent_login = new Intent(); i ...