Skip to content

Commit

Permalink
Add another method to see the SQL generated
Browse files Browse the repository at this point in the history
  • Loading branch information
b0uh committed Feb 28, 2024
1 parent f74686a commit 4abe7ce
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
19 changes: 14 additions & 5 deletions django-show-me-the-sql.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="Thomas Loiret">
<meta name="description" content="6 different ways to see the SQL generated by Django.">
<meta name="description" content="7 different ways to see the SQL generated by Django">

<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@b0uh">
<meta name="twitter:creator" content="@b0uh">
<meta name="twitter:title" content="Django: show me the SQL - Thomas Loiret - Random thoughts">
<meta name="twitter:description" content="6 different ways to see the SQL generated by Django.">
<meta name="twitter:description" content="7 different ways to see the SQL generated by Django">
<meta name="twitter:image" content="https://b0uh.github.io/medias/show-me-what-you-got-rick-and-morty.png">

<meta name="google-site-verification" content="jZBc2U9MHiBnSlgpoFEed-V3PZ16pMNv0GSX4hOh4mg" />
Expand Down Expand Up @@ -165,12 +165,13 @@ <h1 itemprop="name">Django: show me the SQL</h1>
</div>

<span itemprop="articleBody">
<p>Here are 6 different ways of inspecting the SQL generated by the ORM of Django.</p>
<p>Here are 7 different ways of inspecting the SQL generated by the ORM of Django.</p>
<ol>
<li><a href="#anchor-using-query-attribute">Using the <code>query</code> attribute</a></li>
<li><a href="#anchor-using-connection-queries">Using <code>connection.queries</code></a></li>
<li><a href="#anchor-in-the-logs">In the logs</a></li>
<li><a href="#anchor-using-django-debug-toolbar">Using Django Debug Toolbar</a></li>
<li><a href="#anchor-using-django-debug-toolbar-debugsqlshell">Using Django Debug Toolbar and debugsqlshell</a></li>
<li><a href="#anchor-django-extensions-shell-plus">Using django-extensions and shell_plus</a></li>
<li><a href="#anchor-django-extentions-runserver-plus">Using django-extensions and runserver_plus</a></li>
</ol>
Expand Down Expand Up @@ -217,16 +218,24 @@ <h1>In the logs<a id="anchor-in-the-logs"></a></h1>

<p>Tip: add debug log in your code to find where SQL is coming from. The logs</p>
<h1>Using Django Debug Toolbar<a id="anchor-using-django-debug-toolbar"></a></h1>
<p>The initial effort to install the toolbar is a bit higher than the other method. But when working on a website, this is a must have.</p>
<p>The initial effort to install the toolbar is a bit higher than the other methods. But when working on a website, this is a must have.</p>
<ol>
<li>Install it following the instructions <a href="https://django-debug-toolbar.readthedocs.io/en/latest/installation.html">here</a></li>
<li>Open the page you want to inspect in your browser and check out the "SQL" section.</li>
</ol>
<h1>Using Django Debug Toolbar and debugsqlshell<a id="anchor-using-django-debug-toolbar-debugsqlshell"></a></h1>
<ol>
<li>Install it following the instructions <a href="https://django-debug-toolbar.readthedocs.io/en/latest/installation.html">here</a></li>
<li><code>./manage.py debugsqlshell</code></li>
<li>In the shell do a query, for example: <code>User.objects.count()</code></li>
</ol>
<p>This method also display the time the query took to be executed which can be useful.</p>
<p>Official documentation: <a href="https://django-debug-toolbar.readthedocs.io/en/latest/commands.html#debugsqlshell">here</a></p>
<h1>Using django-extensions and shell_plus<a id="anchor-django-extensions-shell-plus"></a></h1>
<ol>
<li><code>python -m pip install django-extensions</code></li>
<li><code>./manage.py shell_plus --print-sql</code></li>
<li>In the shell do a query, for example <code>User.objects.count()</code></li>
<li>In the shell do a query, for example: <code>User.objects.count()</code></li>
</ol>
<p>Official documentation: <a href="https://django-extensions.readthedocs.io/en/latest/shell_plus.html#sql-queries">here</a></p>
<h1>Using django-extensions and runserver_plus<a id="anchor-django-extentions-runserver-plus"></a></h1>
Expand Down
15 changes: 12 additions & 3 deletions feeds/all.atom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Thomas Loiret - Random thoughts</title><link href="https://b0uh.github.io/" rel="alternate"></link><link href="https://b0uh.github.io/feeds/all.atom.xml" rel="self"></link><id>https://b0uh.github.io/</id><updated>2024-02-28T13:00:00+01:00</updated><entry><title>Django: show me the SQL</title><link href="https://b0uh.github.io/django-show-me-the-sql.html" rel="alternate"></link><published>2024-02-28T13:00:00+01:00</published><updated>2024-02-28T13:00:00+01:00</updated><author><name>Thomas Loiret</name></author><id>tag:b0uh.github.io,2024-02-28:/django-show-me-the-sql.html</id><summary type="html">&lt;p&gt;6 different ways to see the SQL generated by Django.&lt;/p&gt;</summary><content type="html">&lt;p&gt;Here are 6 different ways of inspecting the SQL generated by the ORM of Django.&lt;/p&gt;
<feed xmlns="http://www.w3.org/2005/Atom"><title>Thomas Loiret - Random thoughts</title><link href="https://b0uh.github.io/" rel="alternate"></link><link href="https://b0uh.github.io/feeds/all.atom.xml" rel="self"></link><id>https://b0uh.github.io/</id><updated>2024-02-28T13:00:00+01:00</updated><entry><title>Django: show me the SQL</title><link href="https://b0uh.github.io/django-show-me-the-sql.html" rel="alternate"></link><published>2024-02-28T13:00:00+01:00</published><updated>2024-02-28T13:00:00+01:00</updated><author><name>Thomas Loiret</name></author><id>tag:b0uh.github.io,2024-02-28:/django-show-me-the-sql.html</id><summary type="html">&lt;p&gt;7 different ways to see the SQL generated by Django&lt;/p&gt;</summary><content type="html">&lt;p&gt;Here are 7 different ways of inspecting the SQL generated by the ORM of Django.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="#anchor-using-query-attribute"&gt;Using the &lt;code&gt;query&lt;/code&gt; attribute&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#anchor-using-connection-queries"&gt;Using &lt;code&gt;connection.queries&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#anchor-in-the-logs"&gt;In the logs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#anchor-using-django-debug-toolbar"&gt;Using Django Debug Toolbar&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#anchor-using-django-debug-toolbar-debugsqlshell"&gt;Using Django Debug Toolbar and debugsqlshell&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#anchor-django-extensions-shell-plus"&gt;Using django-extensions and shell_plus&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#anchor-django-extentions-runserver-plus"&gt;Using django-extensions and runserver_plus&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
Expand Down Expand Up @@ -51,16 +52,24 @@

&lt;p&gt;Tip: add debug log in your code to find where SQL is coming from. The logs&lt;/p&gt;
&lt;h1&gt;Using Django Debug Toolbar&lt;a id="anchor-using-django-debug-toolbar"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;p&gt;The initial effort to install the toolbar is a bit higher than the other method. But when working on a website, this is a must have.&lt;/p&gt;
&lt;p&gt;The initial effort to install the toolbar is a bit higher than the other methods. But when working on a website, this is a must have.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Install it following the instructions &lt;a href="https://django-debug-toolbar.readthedocs.io/en/latest/installation.html"&gt;here&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Open the page you want to inspect in your browser and check out the "SQL" section.&lt;/li&gt;
&lt;/ol&gt;
&lt;h1&gt;Using Django Debug Toolbar and debugsqlshell&lt;a id="anchor-using-django-debug-toolbar-debugsqlshell"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;Install it following the instructions &lt;a href="https://django-debug-toolbar.readthedocs.io/en/latest/installation.html"&gt;here&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;./manage.py debugsqlshell&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;In the shell do a query, for example: &lt;code&gt;User.objects.count()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This method also display the time the query took to be executed which can be useful.&lt;/p&gt;
&lt;p&gt;Official documentation: &lt;a href="https://django-debug-toolbar.readthedocs.io/en/latest/commands.html#debugsqlshell"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;Using django-extensions and shell_plus&lt;a id="anchor-django-extensions-shell-plus"&gt;&lt;/a&gt;&lt;/h1&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;python -m pip install django-extensions&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;./manage.py shell_plus --print-sql&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;In the shell do a query, for example &lt;code&gt;User.objects.count()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;In the shell do a query, for example: &lt;code&gt;User.objects.count()&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Official documentation: &lt;a href="https://django-extensions.readthedocs.io/en/latest/shell_plus.html#sql-queries"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;Using django-extensions and runserver_plus&lt;a id="anchor-django-extentions-runserver-plus"&gt;&lt;/a&gt;&lt;/h1&gt;
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ <h2 class="front-page-title">
</span>
</a>
</h2>
<p itemprop="articleSection"><p>6 different ways to see the SQL generated by Django.</p></p>
<p itemprop="articleSection"><p>7 different ways to see the SQL generated by Django</p></p>
</div>
<!-- End of the content -->
<a href="https://b0uh.github.io/django-show-me-the-sql.html" itemprop="url">
Expand Down
2 changes: 1 addition & 1 deletion sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

<url>
<loc>https://b0uh.github.io/</loc>
<lastmod>2024-02-28T11:46:40-00:00</lastmod>
<lastmod>2024-02-28T13:20:38-00:00</lastmod>
<changefreq>monthly</changefreq>
<priority>0.5</priority>
</url>
Expand Down

0 comments on commit 4abe7ce

Please sign in to comment.