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同时赋值给了多个变量
随机推荐
- 每天一个linux命令(1):which命令(转)
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索: which 查看可执行文件的位置. whereis 查看文件的位置. locate ...
- Eclispe使用Maven添加官方库的jar包
先到百度或google搜索maven仓库,在仓库中搜索需要的jar包,如poi.jar. 搜索到之后找到需要的jar包,找到这里
- 通过inotify监控linux文件系统变化
http://www.mjmwired.net/kernel/Documentation/filesystems/inotify.txt http://www.ibm.com/developerwor ...
- CodeIgniter(CI 3.0)分页类实践记录
最近在学习B/S,选择了PHP CI框架作为切入点. 在尝试制作个人CMS的时候遇到了需要分页的情况,网上好像搜不到3.0版本以上的例子,下面附上本地实验的代码,供参考. 数据库情况如下: 首先看Co ...
- Memcached 配置 和项目应用
Memcached 配置 http://blog.csdn.net/sup_heaven/article/details/32337711 memcached真实项目中的应用 http://blog. ...
- CSS3 text-rendering属性
这种非标准的属性目前不被推荐.我们一般会找一个可以替代的方法来完成相同的功能,不到外不得已,最好别用. CSS的这个text-rendering属性通常被用在Windows和Linux系统中,用来 ...
- 初步了解SequoiaDB数据库
随着企业中日益复杂与多变的需求,以及迅速扩展带来的海量数据的业务,IT部门需要将越来越多的信息提供给用户,同时在现今的全球经济背景环境下,IT部 门还需要在提供高效服务的同时,降低其设备与程序维护成本 ...
- Discuz X3.2 SEO设置 title 不支持空格的解决方法
很多使用 Discuz X3.2 的同学都发现这么一个问题:在后台SEO设置-title设定的时候,即使你在连字符两侧输入了空格,在前台也显示不出来,很多同学纠结这个问题,今天终于找到了解决方法,在此 ...
- Linq中延迟查询和立即查询
//立即查询 public static void NowExecute() { var results = new int[]{5,4,3,2,1,6,7,8,9,0 }; int i = 0; v ...
- cadence PCB绘制步骤
1 创建一个PCB文件 file -> new 2 创建一个板框 add -> line ,在 options 选型中选择好,板框为 长 4400mil 宽 3200 3 给PCB板框 ...