One simple step to improve your Django skills — use django-debug-toolbar

Beda Kosata
3 min readJul 2, 2021

I recently conducted more than a dozen interviews for a Django web-developer position. The interviewees were mostly self-proclaimed experienced Django developers. However, I found that most of them lacked basic knowledge of how Django executes queries, especially when it comes to dealing with foreign keys.

I consider this knowledge fundamental to preventing the N+1 query problem (I will not go into detail here, maybe in some later article) which may creep into your code without you realizing it. Even if you are an experienced developer aware of it. When you are not aware of it, your code will most likely be full of such cases.

This article is not about solving the N+1 query problem or similar database related issues. It is about even spotting them, as they can be very easily overlooked hidden behind the easy to use ORM.

The reason why I am mentioning the N+1 query problem is that it is very common and very easy to prevent — if you use the right tools. And django-debug-toolbar is one of them. (BTW, not one of the aforementioned developers used it. No wonder — if they had, they couldn’t have stayed unaware of the problem for long. Who knows, they might even have landed a great job ;))

One of the most useful features of the django-debug-toolbar is that it makes it very easy to review the queries a view makes. The total number of queries is visible immediately and a more detailed overview is available upon request.

Easily spot that 53 out of 56 queries are similar. A tell-tale sign of the N+1 query problem.

Checking the number of queries and reviewing them for potential duplicates is a routine part of development. When you are rendering a page with 50 objects and see more than 50 queries, the N+1 query problem is likely the culprit. You can then easily investigate which queries are repeated over and over to find the right place in your code where to fix it.

But spotting potential N+1 query problems is only of the benefits of reviewing queries. Especially for more junior developers, it can be quite revealing to see what the Django ORM does behind the scenes (see the session and user queries right at the top of the above screenshot?). The toolbar also makes it easy to review the actual SQL run against the server, see their timings and even run EXPLAIN on individual queries.

Easily EXPLAIN a query plan for a troublesome SQL query

So from my point of view, the django-debug-toolbar is the number one tool that a Django developer can use to advance his/her Django skills. It makes the inner workings of Django much more transparent. And the SQL panel is only one part of the toolbar — it also contains other information like the CPU time consumed by the page, cache usage, etc.

If you are not using it already, give it a try.

--

--

Beda Kosata

Python/Django developer (since Python 1.5 and Django 0.96) with background and interests in life sciences.