26.3.5 Introspecting Optimization in the Shell

Using the shell in Django can help us learn not only how to optimize querysets but also how to profile our querysets to improve them.

26.3.5.1 Basic Introspection and Optimization

While the debug-toolbar is an incredibly useful tool for seeing the global state of a webpage created by Django, it is not equipped to examine querysets granularly. Luckily, Django is. Every QuerySet object comes with a query attribute, as shown in Example 26.8, which prints the SQL code that Django uses to get the information from the database.

Example 26.8: Python Interpreter Code

>>> posts = Post.objects.all() >>> print(posts.query)

Because of the length of the actual SQL, I do not put ...

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.