'ManyRelatedManager' object is not iterable
先看下面的代码:
class Worker(models.Model):
departments = moels.ManyToManyField(Department, verbose_name=u"部门列表", blank=True, related_name='workers')
class Department(models.Model):
name = models.CharField(u"名字", max_length=255)
wx_id = models.CharField(u"部门id", max_length=64, null=True, blank=True, db_index=True)
# 执行如下代码
worker = Worker.objects.get(pk=14)
worker.departments = models.Department.objects.filter(wx_id__in=info.get("department"))
for dept in worker.departments:
print(dept.wx_id)
这个时候会报错'ManyRelatedManager' object is not iterable, 原因是worker.departments是不可迭代的,可以使用for dept in worker.departments.all()来迭代
更新ManyToManyField可以用add()方法,add()方法相当于update_or_create,不会重复增加
'ManyRelatedManager' object is not iterable的更多相关文章
- DJANGO问题--Error: ‘ManyRelatedManager’ object is not iterable
http://www.itblah.com/django-error-manyrelatedmanager-object-iterable/ Django: Error: ‘ManyRelatedMa ...
- 'NoneType' object is not iterable
"TypeError: 'NoneType' object is not iterable" 一般是返回值为None同时赋值给了多个变量
- Python问题:'Nonetype' object is not iterable
参考链接:http://blog.csdn.net/dataspark/article/details/9953225 [解析] 这个错误提示一般发生在将None赋给多个值时. [案例] 定义了如下的 ...
- 'WebElement' object is not iterable
checkbox.html源码: <html> <head> <meta http-equiv="content-type" content=&quo ...
- TypeError: 'ExcelData' object is not iterable
今天写了个测试的代码,结果在执行test_register.py文件在调用readexcle.py的时候一直报错TypeError: 'ExcelData' object is not iterabl ...
- python TypeError: 'NoneType' object is not iterable
list(set(map(lambda tp_id : tp_id if not ('#' in tp_id) and len(tp_id.strip().replace('\n', '')) > ...
- TypeError: 'TestCase' object is not iterable
这个异常呢其实是因为我对list没有足够熟悉 我一开始很疑惑,明明已经正确返回testcase对象了呀,为啥会报TypeError: 'TestCase' object is not iterable ...
- python报错Nonetype object is not iterable
https://www.cnblogs.com/zhaijiahui/p/8391701.html 参考链接:http://blog.csdn.net/dataspark/article/detail ...
- for遍历用例数据时,报错:TypeError: list indices must be integers, not dict,'int' object is not iterable解决方法
一:报错:TypeError: list indices must be integers, not dict for i in range(0,len(test_data)): suite.addT ...
随机推荐
- css让图片居中显示在手机屏幕上
直接上代码了 <div class="showpic"> <span></span> <img src="" alt= ...
- ZOJ 3745 Salary Increasing
Description Edward has established a company with n staffs. He is such a kind man that he did Q time ...
- UESTC_Big Brother 2015 UESTC Training for Graph Theory<Problem G>
G - Big Brother Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) ...
- UVA_埃及分数(Hard Version) UVA 12588
Problem EEg[y]ptian Fractions (HARD version)Given a fraction a/b, write it as a sum of different Egy ...
- 微型 Python Web 框架 Bottle - Heroin blog
微型 Python Web 框架 Bottle - Heroin blog 微型 Python Web 框架 Bottle
- GridBagLayout练习
摘自http://blog.csdn.net/qq_18989901/article/details/52403737 GridBagLayout的用法 GridBagLayout是面板设计中最复杂 ...
- hdu 1078 FatMouse and Cheese 记忆化dp
只能横向或竖向走...一次横着竖着最多k步...不能转弯的.... 为毛我的500+ms才跑出来... #include<cstdio> #include<iostream> ...
- 【v2.x OGE教程 20】粒子效果
1.介绍 粒子系统表示三维计算机图形学中模拟一些特定的模糊现象的技术.而这些现象用其他传统的渲染技术难以实现的真实感的 game physics.常常使用粒子系统模拟的现象有火.爆炸.烟.水流.火花. ...
- JavaScript面向对象精要(一)
数据类型 在JavaScript中,数据类型分为两类: 原始类型:保存一些简单数据,如true.5等.JavaScript共同拥有5中原始类型: boolean:布尔.值为true或false num ...
- Web页面布局方式小结
Web页面是由块元素组成的,正常情况下块元素一个个按垂直方向排布,构成了页面.可是这样的主要的布局方式绝大多时候不能满足我们的需求,所以各种布局方式应运而生,本文就对这些布局方式做个小结. 1.元素漂 ...