Django Show the Raw SQL Query Running

To see the raw SQL queries Django is running, do the following:

In your settings file (typically, ensure DEBUG is set to True

DEBUG = True

Then import connection from django.db and print connection.queries

import pprint
from django.db import connection

If your queries are slowing you down, look into using select_related() to follow foreign keys

Did this help you? Please let me know with a comment. Thank you
Posted .

4 comments (RSS)

  1. anonymous

    see also debugsqlshell in django-debug-toolbar.

  2. anonymous


    import logging

    l = logging.getLogger('django.db.backends')



  3. anonymous


    >>> queryset = MyModel.objects.all()

    >>> print queryset.query

    SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel"

  4. anonymous

    from django.db import connection

    print connection.queries

    works for the update() command as well.

Leave a comment