Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3632432
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:31:28+00:00 2026-05-19T00:31:28+00:00

How do you view the SQL generated by Django for a DELETE ? When

  • 0

How do you view the SQL generated by Django for a DELETE?

When doing a SELECT operation on a query set, you can do this:

>>> qs = Entry.objects.filter(date__gt='2010-06-01')
>>> qs.query.as_sql()
('SELECT ...)

But I don’t know how to get the SQL for what happens when I do qs.delete().

It looks a bit more involved because Django “emulates the behavior of the SQL constraint ON DELETE CASCADE” when deleting objects.

(Background: trying to debug an IntegrityError generated by a foreign key constraint when deleting a subclassed model object.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:31:28+00:00Added an answer on May 19, 2026 at 12:31 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I view the SQL code of my query after it has been
How do I view the SQL that is generated by nHibernate? version 1.2
If a WITH NOLOCK query hint is used on a View in SQL Server,
What SQL query shows me the tables & indexes used by a view on
I am trying to view SQL generated by Linq to SQL in the SQL
How do I view the SQL generated by entity framework ? (In my particular
Is there an Ibatis setting to view the generated SQL like how Hibernate has
Is there a simple way to view the SQL Queries actually generated by SSRS
I am creating an SQL view for a file that strips out the spaces
When I create a view in SQL Server 2005 and someone later opens it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.