=Start=
缘由:
在使用的过程中记录一下Django的ORM知识/技巧,一来可以加深印象,同时也为方便以后参考。
正文:
1.在Django的ORM中如何访问存储过程?
from django.db import connection cursor = connection.cursor() cursor.execute("SQL STATEMENT CAN BE ANYTHING") #cursor.fetchone() cursor.fetchall()
- http://stackoverflow.com/questions/805393/what-is-the-best-way-to-access-stored-procedures-in-djangos-orm
- https://docs.djangoproject.com/en/1.8/topics/db/sql/#performing-raw-queries
2.对象关系映射器
- http://fullstackpython.atjiang.com/object-relational-mappers-orms.html
- https://www.fullstackpython.com/object-relational-mappers-orms.html
3.为什么Django的ORM比原生的MySQLdb慢?
在大查询下,比如报表业务,不建议使用ORM,因为速度比较慢,原因在于:它会把结果转换成QuerySet结构,而不是直接返回;所以这时可以考虑用原生的SQL语句。
在Python下可以选择 MySQLdb ,也可以用Django的connection/connections。推荐用connection/connections,因为数据库配置等信息都已经放在了settings.py里面。
from django.db import connection, transaction import MySQLdb # ... cursor = connection.cursor(cursorclass = MySQLdb.cursors.DictCursor) cursor.execute(sql, None) # 如果你有多个数据库,可以在connections中选择数据库: from django.db import connections cursor = connections['crazyof'].cursor() transaction.commit_unless_managed(using='crazyof')
4.获取Django ORM query执行的的SQL语句
# 方法一 queryset = MyModel.objects.all() print 'Query SQL: {0}'.format(queryset.query) # 方法二(debug=True) from django.db import connections print connections['default'].queries # 方法三 # 利用三方提供的 middleware 来实现,参考: https://djangosnippets.org/snippets/290/ # 是插拔式的,不需要的时候在 settings.py 中去掉这个middleware就可以了,不过这只能在debug 模式下使用
5.在Django的ORM中如何判断查询结果是否为空
my_queryset = Booking.objects.filter(date_select='2011-12-2') #方法一 .exists() if my_queryset.exists(): print "QuerySet has Data" else: print "QuerySet is empty" #方法二 .count()==0 if my_queryset.count() == 0: print "empty" #方法三 if my_queryset: print "QuerySet has Data" else: print "QuerySet is empty" 总结: QuerySet.exists() > QuerySet.count()==0 > QuerySet
- https://docs.djangoproject.com/en/1.8/topics/db/optimization/#use-queryset-count
- http://stackoverflow.com/questions/8365255/how-to-specify-empty-queryset-to-not-empty-queryset-to-object-hi
- http://stackoverflow.com/questions/9669853/how-can-i-test-for-an-empty-queryset-in-django
- https://docs.djangoproject.com/en/1.8/ref/models/querysets/#django.db.models.query.QuerySet.all
- http://stackoverflow.com/questions/1387727/checking-for-empty-queryset-in-django
6.在Python的MySQLdb模块中如何判断查询结果是否为空
cursor = connections['security'].cursor() if sql_params: cursor.execute(sql_str, sql_params) else: cursor.execute(sql_str) if cursor.rowcount: aaData = [] for item in cursor: aaData.append(list(item)) return JsonResponse({'aaData': aaData}) else: return JsonResponse({'aaData': []})
- http://stackoverflow.com/questions/16561362/python-how-to-check-if-a-result-set-is-empty
- http://stackoverflow.com/questions/19382396/print-if-mysql-returns-no-results
7.待添加
……
=END=
《 “Django的ORM使用记录_3” 》 有 4 条评论
优化Django ORM中的性能问题
http://blog.csdn.net/orangleliu/article/details/57088557
https://medium.com/@hansonkd/performance-problems-in-the-django-orm-1f62b3d04785#.d6v5u1f8l
Django db使用MySQL连接池
https://zhu327.github.io/2016/09/25/django-db%E4%BD%BF%E7%94%A8mysql%E8%BF%9E%E6%8E%A5%E6%B1%A0/
Django 基于 Postgres 的全文搜索
https://juejin.im/entry/593a069b61ff4b006c70ba82
http://blog.lotech.org/postgres-full-text-search-with-django.html
Django+Elasticsearch实现搜索功能
https://zhu327.github.io/2016/05/30/django+elasticsearch%E5%AE%9E%E7%8E%B0%E6%90%9C%E7%B4%A2%E5%8A%9F%E8%83%BD/
Django db.reset_queries
django 数据库连接模块解析及简单长连接改造
http://blog.csdn.net/u010477231/article/details/77576357
使django与数据库保持长连接
https://www.cnblogs.com/descusr/p/3566935.html
django内存泄露谨记
http://blog.csdn.net/yangyihongyangjiying/article/details/42212349
『GCTT 出品』如何写 Go 中间件
https://mp.weixin.qq.com/s/gIbwbrvt4JMmYxnZA_Q2Bg
https://kev.inburke.com/kevin/how-to-write-go-middleware/
如何用Python解析SQL
https://stackoverflow.com/questions/1394998/parsing-sql-with-python
如何用Python的sqlparse来分析SQL
https://blog.csdn.net/qq_39607437/article/details/79620383
如何从SQL查询中提取表名和列名?
https://stackoverflow.com/questions/35624662/how-to-extract-table-names-and-column-names-from-sql-query