先看下面的代码:

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

  1. DJANGO问题--Error: ‘ManyRelatedManager’ object is not iterable

    http://www.itblah.com/django-error-manyrelatedmanager-object-iterable/ Django: Error: ‘ManyRelatedMa ...

  2. 'NoneType' object is not iterable

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

  3. Python问题:'Nonetype' object is not iterable

    参考链接:http://blog.csdn.net/dataspark/article/details/9953225 [解析] 这个错误提示一般发生在将None赋给多个值时. [案例] 定义了如下的 ...

  4. 'WebElement' object is not iterable

    checkbox.html源码: <html> <head> <meta http-equiv="content-type" content=&quo ...

  5. TypeError: 'ExcelData' object is not iterable

    今天写了个测试的代码,结果在执行test_register.py文件在调用readexcle.py的时候一直报错TypeError: 'ExcelData' object is not iterabl ...

  6. 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', '')) > ...

  7. TypeError: 'TestCase' object is not iterable

    这个异常呢其实是因为我对list没有足够熟悉 我一开始很疑惑,明明已经正确返回testcase对象了呀,为啥会报TypeError: 'TestCase' object is not iterable ...

  8. python报错Nonetype object is not iterable

    https://www.cnblogs.com/zhaijiahui/p/8391701.html 参考链接:http://blog.csdn.net/dataspark/article/detail ...

  9. 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 ...

随机推荐

  1. poi操作excel设置数据有效性

    private void setDataValidationList(short firstRow,short endRow,short firstCol, short endCol,String d ...

  2. CSS3前缀

    我们经常会遇到写的有些css3属性会在不同的浏览器下呈现不兼容的情况,那是因为浏览器内核不同而导致的兼容性问题. 首先我们需要先了解一下目前的几种现代浏览器的内核,主流内容主要有Mozilla(熟悉的 ...

  3. Python学习(七) 流程控制if语句

    在Python中流程控制if语句采用如下格式: if expression : statement elif expression : statement elif expression : stat ...

  4. linux curses函数库

    fedora20,安装yum install ncurses-devel 编译时:-lncurses 头文件:#include<curses.h> 参考:man ncurses \linu ...

  5. Nginx的Upload上传模块

    前段时间做一个项目,需要上传文件,差不多需要20M左右,普通用php处理会比较麻烦,经常超时,而且大量占用资源.于是搜索了下,决定用nginx的upload上传模块来处理. 你可以在这里:http:/ ...

  6. 关于《s3c2416裸跑环境配置》一文的一些补充

    <s3c2416裸跑环境配置>一文已经发表很长一段时间了,前两天突然收到邮件提示有人回复,原来网友jxyggg按照文中所讲去调试,却始终不能成功.问题的描述见原文后的回复,经过QQ交流,问 ...

  7. 【转】如何设置无线路由器的信道以获得最佳WIFI体验?

    原文网址:http://jingyan.baidu.com/album/f25ef2546e28e4482c1b8225.html 现在随着移动互联网的发展,移动终端的普及,WIFI越来越必不可少,所 ...

  8. java MD5加密

    public final static String MD5(String s)    {        char hexDigits[] =        { '0', '1', '2', '3', ...

  9. postgresql基本语句

    preface,熟悉pgsql sql Language article disorder; 1,pgsql数据库控制台Cli(command line interface) help mannual ...

  10. XMPP通讯开发-仿QQ显示好友列表和用户组

    在 XMPP通讯开发-服务器好友获取以及监听状态变化   中我们获取服务器上的用户好友信息,然后结合XMPP通讯开发-好友获取界面设计    我们将两个合并起来,首先获取用户组,然后把用户组用List ...