'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 ...
随机推荐
- ThinkPHP框架一
1.1 框架的概念 框架其实就是可重用代码的集合,框架的代码是框架架构的代码,不是业务逻辑代码,框架代码保护类.方法.函数等等,框架代码按照一定的规则组合起来就形成了框架. 1.2 不使用框架开发的时 ...
- 远程复制 scp命令
定义 本机为A,用户名为usera,登录远程主机B的为userb,IP为remote_ip 1. 从B 拷贝文件到A机器 用下面的命令 scp userb@remote_ip:remote_path ...
- java 数字补齐0
String str_f = str.substring(0, 1); int i = (Integer.parseInt(str.substring(1)) + 1); // 数字补齐0 Decim ...
- python运维开发(十八)----Django(二)
内容目录 路由系统 模版 Ajax model数据库操作,ORM 路由系统 django中的路由系统和其他语言的框架有所不同,在django中每一个请求的url都要有一条路由映射,这样才能将请求交给对 ...
- awk之7 常用函数的解析
1.区域获取 substr(区域f,起始位置n1,获取范围n2) 解析:获取某个区域f内,从起始位置n1开始算起的n2个字符组成的字符串.如果n2不存在,则返回从n1开始到区域结束的内容. 例子:获取 ...
- Tempter of the Bone(dfs+奇偶剪枝)
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- Why Does Qt Use Moc for Signals and Slots(QT官方的解释:GUI可以是动态的)
GUIs are Dynamic C++ is a standarized, powerful and elaborate general-purpose language. It's the onl ...
- C# 委托2
委托的定义: (1) 将方法作为变量使用的一种机制,就是将方法当作变量用(声明,赋值,传参) (2) 将变量当作方法来用,首先就要去声明变量,就要考虑变量的类型,就是(委托变量,对应方法的返回值, ...
- mCustomScrollbar的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- C# Cookie编程
Cookie,他最早出现是在Netscape Navigator 2.0中.Cookie其实就是由Web服务器创建的.将信息存储在机上的文件.那么为什么Web服务器要在客户机上面创建如此文件?这是因为 ...