Search code examples
djangodjango-orm

Can Django's ORM output the SQL query it is using?


I know that you can output the SQL to see tables that are created. Is it possible for Django to output the sql used for any query like:

Protocols.objects.filter(active=False)

? I couldn't find this in the docs, so hopefully someone can point them to me, if in fact Django can do this.


Solution

  • See Django FAQ: How can I see the raw SQL queries Django is running?:

    >>> from django.db import connection    
    >>> connection.queries = []
    >>> Protocols.objects.filter(active=False)
    >>> print(connection.queries)