Django Get SQL for Django queryset


Example

The query attribute on queryset gives you SQL equivalent syntax for your query.

>>> queryset = MyModel.objects.all()
>>> print(queryset.query)
SELECT "myapp_mymodel"."id", ... FROM "myapp_mymodel"

Warning:

This output should only be used for debugging purposes. The generated query is not backend-specific. As such, the parameters aren't quoted properly, leaving it vulnerable to SQL injection, and the query may not even be executable on your database backend.