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.
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.
>>> posts = Post.objects.all() >>> print(posts.query)
Because of the length of the actual SQL, I do not put ...