diff --git a/django-show-me-the-sql.html b/django-show-me-the-sql.html index ce1990e..4ed9e31 100644 --- a/django-show-me-the-sql.html +++ b/django-show-me-the-sql.html @@ -5,13 +5,13 @@ - + - + @@ -165,12 +165,13 @@

Django: show me the SQL

-

Here are 6 different ways of inspecting the SQL generated by the ORM of Django.

+

Here are 7 different ways of inspecting the SQL generated by the ORM of Django.

  1. Using the query attribute
  2. Using connection.queries
  3. In the logs
  4. Using Django Debug Toolbar
  5. +
  6. Using Django Debug Toolbar and debugsqlshell
  7. Using django-extensions and shell_plus
  8. Using django-extensions and runserver_plus
@@ -217,16 +218,24 @@

In the logs

Tip: add debug log in your code to find where SQL is coming from. The logs

Using Django Debug Toolbar

-

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.

+

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.

  1. Install it following the instructions here
  2. Open the page you want to inspect in your browser and check out the "SQL" section.
+

Using Django Debug Toolbar and debugsqlshell

+
    +
  1. Install it following the instructions here
  2. +
  3. ./manage.py debugsqlshell
  4. +
  5. In the shell do a query, for example: User.objects.count()
  6. +
+

This method also display the time the query took to be executed which can be useful.

+

Official documentation: here

Using django-extensions and shell_plus

  1. python -m pip install django-extensions
  2. ./manage.py shell_plus --print-sql
  3. -
  4. In the shell do a query, for example User.objects.count()
  5. +
  6. In the shell do a query, for example: User.objects.count()

Official documentation: here

Using django-extensions and runserver_plus

diff --git a/feeds/all.atom.xml b/feeds/all.atom.xml index e36c1a9..d5031f2 100644 --- a/feeds/all.atom.xml +++ b/feeds/all.atom.xml @@ -1,10 +1,11 @@ -Thomas Loiret - Random thoughtshttps://b0uh.github.io/2024-02-28T13:00:00+01:00Django: show me the SQL2024-02-28T13:00:00+01:002024-02-28T13:00:00+01:00Thomas Loirettag:b0uh.github.io,2024-02-28:/django-show-me-the-sql.html<p>6 different ways to see the SQL generated by Django.</p><p>Here are 6 different ways of inspecting the SQL generated by the ORM of Django.</p> +Thomas Loiret - Random thoughtshttps://b0uh.github.io/2024-02-28T13:00:00+01:00Django: show me the SQL2024-02-28T13:00:00+01:002024-02-28T13:00:00+01:00Thomas Loirettag:b0uh.github.io,2024-02-28:/django-show-me-the-sql.html<p>7 different ways to see the SQL generated by 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> @@ -51,16 +52,24 @@ <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> diff --git a/index.html b/index.html index 11f5dc2..a21faf4 100644 --- a/index.html +++ b/index.html @@ -163,7 +163,7 @@

-

6 different ways to see the SQL generated by Django.

+

7 different ways to see the SQL generated by Django