How can I inspect the underlying SQL statements executed by Django?

In many Django projects, it is essential to examine the exact SQL commands that are executed when performing queries. I am looking for methods or built-in techniques in Django that allow developers to capture, log, or review these database queries for debugging and performance tuning. Understanding these SQL operations can help diagnose inefficiencies or issues within the ORM. Detailed examples or illustrative code snippets would be greatly appreciated to help me uncover and analyze the SQL generated during runtime.

I have found that enabling Django’s debug mode is a straightforward method to monitor the SQL statements executed during request processing. This approach, combined with the built-in middleware and logging facilities, provides a live feed of database interactions. In practice, I often supplement these tools with external packages, such as a debug toolbar, to facilitate a deeper examination of SQL performance. Adjusting logger settings for the database backends further refines the output, assisting in the rapid detection of inefficiencies in complex queries.