text: Opened 8 years ago
  Closed 8 years ago
  Last modified 6 years ago
  #13101 closed (fixed)
  Documentation: Add note about multiple databases and debugging raw sql
  Description has a section called "How can I see the raw SQL queries Django is running?", where it suggests:
  >>> from django.db import connection >>> connection.queries [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls', 'time': '0.002'}]
  However, in a multidb scenario, that only gets the sql from the default connection. I found a better way is this function:
  def get_sql(): from django import db q = [] for c in db.connections.all(): q.extend(c.queries) return q
  A note should probably be made about this.
  Attachments (1)
  Change History (8)
  comment:1 Changed 8 years ago by
  comment:2 Changed 8 years ago by
  Changed 8 years ago by
  Updated the attached file with Russell's proposal
  comment:3 Changed 8 years ago by
  comment:4 Changed 8 years ago by
  Adding myself as cc in case more is required
  comment:5 Changed 8 years ago by
  comment:6 Changed 8 years ago by
  comment:7 Changed 6 years ago by
  Milestone 1.2 deleted
  Note: See TracTickets for help on using tickets.
  There is an even better way:
  connections['mydb'].queries. Notes about this approach were added in r12709, but not to the FAQ.