Django Querysets Get SQL for Django queryset

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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.



Got any Django Question?