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 %}

时间: 2024-09-12 05:56:06

DJANGO问题--Error: ‘ManyRelatedManager’ object is not iterable的相关文章

webpy &amp;amp;#39;builtin_function_or_method&amp;amp;#39; object is not iterable

问题描述 webpy 'builtin_function_or_method' object is not iterable 我准备用webpy做一个网站,现在遇到这个困难,希望知道的帮忙一下, 我的网站结构: mysite --site_helper.py --controllers/index.py --templates/main/base.html --templates/main/index.html site_helper.py #! /usr/bin/python #-*- cod

报表报错net.sf.jasperreports.engine.JRException: Error loading object from file

问题描述 Servlet代码privateFileAttachServicefileAttachService=(FileAttachService)AppUtil.getBean("fileAttachService");privateLoglogger=LogFactory.getLog(FilePreviewServlet.class);protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsS

记录Django开发心得_python

概念层面 概括 Django是属于MVC的Web框架. Model:负责与数据库打交道 View:负责获取或者增强从Models得到的数据 Controller:这是Django本身 Project与App的区别 Project:提供各种配置文件 App:功能的结合,包括Model和view,需要在根目录下添加"__init__.py",使得Python可以识别 ManyToMany与ForeignKey的区别 ForeignKey例子: 员工打卡上班的记录--员工可有多次打卡记录,但

【Oracle】-【权限-ORA-04043】- ORA-04043: object &quot;SYS&quot;.&quot;V_$DATABASE&quot; does not exist

用非dba账号(但赋予了DBA角色)登录一个新的10g数据库想看下版本号, SQL> desc v$instance; ERROR: ORA-04043: object "SYS"."V_$INSTANCE" does not exist 奇怪,之前有个9i的库,同样的账号就可以查看呢??? 分析: 这里显示的是"SYS"."V_$INSTANCE"对象不存在,我们知道,当账户没有某个对象的权限时,查询该对象,可能返回的

快问快答 | 助教带你学习数据科学(附答疑视频领取)

Q: 可以推荐一本完全零基础的python书看一下吗?我没有数据基础. A: <简明Python教程> Q:在校生,想搞明白未来的职业发展. A:我看数据科学相关的岗位有,比如:数据分析,数据挖掘,机器学习,自然语言处理,计算机视觉,深度学习工程师等等,还有推荐算法.搜索算法. 我觉得咱们的培养目标和数据挖掘.机器学习这两个岗位的要求更贴近.当然,像数据分析.自然语言处理.计算机视觉.深度学习,推荐算法等等这些岗位,就是更专业一点,或者说更垂直一点. 我一直觉得机器学习目前还不是一种通用技术,

跟老齐学Python之有容乃大的list(2)_python

对list的操作 合并list <有容乃大的list(1)>中,对list的操作提到了list.append(x),也就是将某个元素x 追加到已知的一个list后边. 除了将元素追加到list中,还能够将两个list合并,或者说将一个list追加到另外一个list中.按照前文的惯例,还是首先看官方文档中的描述: list.extend(L) Extend the list by appending all the items in the given list; equivalent to a

Python代码解决RenderView窗口not found问题_python

源起 Error:setParent: Object 'renderView' not found 这是一个在工作中很常见的问题,以前做特效的时候有10%的概率会碰到,多发生在打开其他组交接来的Maya场景文件时: 当你不管是从File菜单Open,还是直接拖进Maya都无法解决时.使用下面这段代码即可: 过程 这原本是一段Mel,我用Python把她重写了一下,变成了Python代码~ 根据发展的趋势,Python有可能全面取代Mel. import maya.cmds as mc exist

ASP面向对象编程探讨及比较

比较|编程|对象|比较 ASP是Microsoft于较早期推出的动态网页编程技术,但其结合ADO对数据库方便快捷的访问.结合XML.COM/ActiveX等其它技术 实现服务器多层结构的功能使它在今天还有着顽强的生命力,并且依然有着一定的发展.ASP.Net虽然在架构上完全不同于ASP,但它很多内建对象也是基 于ASP进行扩展的.网上有无数的介绍ASP的文章,却鲜有介绍ASP面向对象以及与其它语言比较的,这也就是我下决心写这篇文章的原因.因为是早期的版本,ASP只提供了很弱的面向对象的接口.

Python_集合set和frozenset的内建方法详解

前言 集合是一种组合型的数据类型,分为可变的set和不可变的frozenset. 软件环境 系统  UbuntuKylin 14.04 软件  Python 2.7.3 IPython 4.0.0 可变集合Set 集合set是一种无序的.唯一的的元素集,与数学中集合的概念类似,可对其进行交.并.差.补等逻辑运算.不支持索引.切片等序列操作,但仍支持成员关系运算符in-not in.推导式等操作.在特定的场合中可以体现出非常优秀的执行效率. set()函数创建集合 set(iterable) ->