Django+MySQL开发记录

2021/8/13 2:06:14

本文主要是介绍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+MySQL开发记录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程