O'Reilly logo

Django Unleashed by Andrew Pinkham

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required