django遇到TypeError: can't multiply sequence by non-int of type 'tuple'
来源:互联网 发布:数控旋压机编程 编辑:程序博客网 时间:2024/06/04 18:03
大家都知道Django默认的数据库用的是sqlite,是的,sqlite用着用着也没什么问题。But,我一次性插入2000条不到的数据竟然花了将近10秒的时间,这怎么能忍呢,于是果断换成了mysql。
人生路漫漫,想转成mysql也不是这么容易的,不转不知道,一转遇到了一大堆问题。现在我分享一个解决了很久的问题。
Django官方的库只支持python2.x版本的mysql驱动,想在windows上用python3+mysql就只能用mysql官方的python驱动。最新支持到python3.4。(注:如果是linux系统,可以尝试mysqlient,Django说可以支持python3,但是windows上装不了这个软件)
转成mysql后,我们首先得执行
python manage.py makemigrations
和
python manage.py migrate
这两句来同步数据库。当执行到最后这一句的时候,遇到了
TypeError: can't multiply sequence by non-int of type 'tuple'
好吧,这里,我试了N种办法,都没有解决,最后没办法,只能修改一下mysql驱动源码了。找到mysql驱动的安装位置里的一个叫做operations.py的文件。比如我的目录是这样的:E:\Python34\Lib\site-packages\mysql\connector\django,如下图这个文件:
打开,查找里面一个叫做bulk_insert_sql的方法,将其修改为如下:
def bulk_insert_sql(self, fields, num_values): # items_sql = "({0})".format(", ".join(["%s"] * len(fields))) # return "VALUES " + ", ".join([items_sql] * num_values) placeholder_rows_sql = (", ".join(row) for row in num_values) values_sql = ", ".join("(%s)" % sql for sql in placeholder_rows_sql) return "VALUES " + values_sql
再重新运行,问题消失!
0 0
- django遇到TypeError: can't multiply sequence by non-int of type 'tuple'
- TypeError: can't multiply sequence by non-int of type 'float'
- 【1】【python】算术运算报错can't multiply sequence by non-int of type 'float'
- django Error during template rendering,TypeError: __str__ returned non-string (type int)
- TypeError: iter() returned non-iterator of type
- TypeError: iter() returned non-iterator of type
- django报错:TypeError: __str__returned non-string (type bytes)
- TypeError: iter() returned non-iterator of type 'Fibs'
- TypeError: argument of type 'int' is not iterable
- invalid initialization of non-const reference of type ‘int*&’ from a temporary of type ‘int*’
- Django查询models对象报错:TypeError: __str__ returned non-string (type decimal.Decimal)
- TypeError: iter() returned non-iterator of type 'xx' 从Python3开始,要用__next__
- python:字符串和数字拼接 (TypeError: Can't convert 'int' object to str implicitly)
- C++之invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’
- C++ : 出错解释 base operand of '->' has non-pointer type 'std::pair<int, int>'
- TypeError: Input 'split_dim' of 'Split' Op has type float32 that does not match expected type of int
- 遇到一个问题 : Can't change tag of fragment CoverFragment
- temporary of type `int'
- 半双工与全双工
- Codeforces 369C Valera And Elections 树形DP
- IE下响应304
- Object.GetType()到底是怎么工作的
- CSS中样式命名tips
- django遇到TypeError: can't multiply sequence by non-int of type 'tuple'
- python之装饰器decorator
- java多线程学习二
- Spark Job调优(Part 1)
- HashMap Hashtable区别
- hadoop第三坑
- 00003 不思议迷宫.0009.4:攻防计算
- 博世传感器调试笔记(一)----加速度传感器BMA253
- HashMap与ConcurrentHashMap的区别