Django+MySQL开发记录

1. Djano 与 MySQL

Django清除数据表中的所有数据

# 异常方法
Book.objects.all().delete()  # 对于大型数据集,可能会失效
Book.objects.raw('''delete from book''')  # 也失效了
文档说明:
Note that delete() is the only QuerySet method that is not exposed on a Manager itself. This is a safety mechanism to prevent you from accidentally requesting Entry.objects.delete(), and deleting all the entries. If you do want to delete all the objects, then you have to explicitly request a complete query set:
#解决方案
book = Book.objects.all()
book.delete()
# 又是返回值的问题。。。。。

Django批量插入mysql数据库,怎么处理NAN值

报错提示:
MySQLdb.err.ProgrammingError: nan can not be used with MySQL
# 解决方案
data.fillna(value=0)  # 用0填充

Django执行sql语句返回数据

data = Book.objects.raw(sql_)

注意:sql_语句中查询的列必须包含主键,data的类型为rawqueryset()

上一篇:Django(19)QuerySet API


下一篇:Django(18)聚合函数