[Fixed]-View the SQL queries for Django queryset delete

20👍

This works well enough:

>>> from django.db import connection
>>> connection.queries[:-10]

Thought the exceptions occurred before the queries were added to connection.queries, but they are indeed present.

Here’s another method which relies on Django internals and doesn’t include queries to do cascading deletes, but doesn’t require executing the query:

from django.db.models import sql

qs = Entry.objects.filter(date__gt='2010-06-01')
query = qs.query.clone()
query.__class__ = sql.DeleteQuery
print(query)

2👍

You could try running django-debug-toolbar and see the queries that way.

👤Tiago

Leave a comment