使用Django扩展插件在Jupyter Notebook中打印SQL查询

编程入门 行业动态 更新时间:2024-10-27 08:39:06
本文介绍了使用Django扩展插件在Jupyter Notebook中打印SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以像在以下命令中那样显示SQL查询:python manage.py shell_plus --print-sql但在Jupyter Notebook中?

Is it possible to show SQL queries like in this command: python manage.py shell_plus --print-sql but in Jupyter Notebook?

我尝试了此命令python manage.py shell_plus --notebook --print-sql,但是没有用.

I tried this command python manage.py shell_plus --notebook --print-sql but it not worked.

推荐答案

这可能是Django Extensions中的一个错误,您没有看到SQL查询.几个版本之前,有人在这里问如何在Jupyter中禁用SQL打印.

It's probably a bug in Django Extensions that you don't see SQL queries. A few versions ago, someone asked here how to disable SQL printing in Jupyter.

作为解决方法,您可以使用 django_print_sql :

As a workaround, you could use django_print_sql:

from django_print_sql import print_sql with print_sql(count_only=False): User.objects.count()

您甚至可能会发现,控制要打印的查询胜于全部打印.

You may even find that having control over which queries to print is preferable to printing all.

但是我主要只是追溯地打印最后一个查询:

But I mostly just print the last query retroactively:

from django import db db.connection.queries[-1]

如果您要使用 sqlparse 漂亮地打印查询,它会变得越来越复杂,以至于实用功能:

If you want to pretty-print the query with sqlparse, it starts getting complicated enough for a utility function:

import sqlparse sqlparse.format( db.connection.queries[-1]['sql'], reindent=True, keyword_case='upper' )

更多推荐

使用Django扩展插件在Jupyter Notebook中打印SQL查询

本文发布于:2023-10-20 22:17:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1512300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:插件   Django   Jupyter   SQL   Notebook

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!