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的更多相关文章

  1. 'ManyRelatedManager' object is not iterable

    先看下面的代码: class Worker(models.Model): departments = moels.ManyToManyField(Department, verbose_name=u& ...

  2. 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( ...

  3. 怎样处理“error C2220: warning treated as error - no object file generated”错误

    最近用VS2010 编译ceflib开源库是出现"怎样处理"error C2220: warning treated as error - no object file gener ...

  4. HBase Error: connection object not serializable

    HBase Error: connection object not serializable 想在spark driver程序中连接HBase数据库,并将数据插入到HBase,但是在spark集群提 ...

  5. unexpected error ConnectionError object has no attribute

    unexpected error ConnectionError object has no attribute

  6. 问题-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 ...

  7. error C2220: warning treated as error - no 'object' file generated解决方法

    error C2220: warning treated as error - no 'object' file generated 警讯视为错误 - 生成的对象文件 / WX告诉编译器将所有警告视为 ...

  8. 安装Django时报错'module' object has no attribute 'lru_cache'

    使用pip方法安装Django时报错'module' object has no attribute 'lru_cache' 解决办法如下 命令行输入命令sudo pip install Django ...

  9. 'NoneType' object is not iterable

    "TypeError: 'NoneType' object is not iterable" 一般是返回值为None同时赋值给了多个变量

随机推荐

  1. bootstrap时间插件 火狐不显示 完美解决方法

    原文链接:http://www.phpbiji.cn/article/index/id/141/cid/4.html bootstrap时间插件火狐 bootstrap-datetimepicker火 ...

  2. Source Insight建工程之Kernel

     不管你是从事于Linux内核工作还是出于兴趣爱好,Linux内核源码都是非常好的学习资源.意味着就要经常的和内核源码大交道,那么软件工具就是少不了的.在Windows系统上确实有着许多好用的软件 ...

  3. 【Qt】Qt之进程间通信(共享内存)【转】

    简述 上一节中,我们分享下如何利用Windows消息机制来进行不同进程间的通信.但是有很多局限性,比如:不能跨平台,而且必须两个进程同时存在才可以,要么进程A发了消息谁接收呢? 下面我们来分享另外一种 ...

  4. js----DOM的三大节点及部分用法

    DOM有三种节点:元素节点.属性节点.文本节点. 一.用nodeType可以检测节点的类型 节点类型 nodeType属性值 元素节点 1 属性节点 2 文本节点 3 这样方便在js中对各个节点进行操 ...

  5. mac OS X下安装Redis及Thinkphp3.1使用Redis

    一.安装Redis 1.安装Homebrew 在终端输入ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/install/maste ...

  6. iblog语法高亮示例

    -------------------------------------------------------------------------------------- iblog 是一款 Sub ...

  7. Python的循环

    循环是一个结构,导致一个程序要重复一定的次数 条件循环也一样,当条件变为假,循环结束 For循环 在python for循环遍历序列,如一个列表或一个字符. for循环语法:   ——for iter ...

  8. linux设备驱动模型

    尽管LDD3中说对多数程序员掌握设备驱动模型不是必要的,但对于嵌入式Linux的底层程序员而言,对设备驱动模型的学习非常重要. Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统 ...

  9. 【linux】

    virtualbox   hyper-v     vmware      KVM      LXC Utils       Docker

  10. Java线程通信——wait() 和 notify()

    Object类中有关线程通信的方法有两个notify方法和三个wait方法,官方解释: void notify() Wakes up a single thread that is waiting o ...