--
Ticket URL: <https://code.djangoproject.com/ticket/17741>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0
Comment:
This isn't intended to give you valid SQL, just a basic representation of
the query.
--
Ticket URL: <https://code.djangoproject.com/ticket/17741#comment:1>
Comment (by anonymous):
Is there another way to get valid sql, without executing the query?
--
Ticket URL: <https://code.djangoproject.com/ticket/17741#comment:2>
Comment (by aaugustin):
There isn't, because Django never actually interpolates the parameters: it
sends the query and the parameters separately to the database adapter,
which performs the appropriate operations.
--
Ticket URL: <https://code.djangoproject.com/ticket/17741#comment:3>
Comment (by zachborboa):
If you need the correct parameter substitution, use `EXPLAIN`. Then remove
the first 8 chars.
{{{#!python
In [1]: import datetime
In [2]: from django.db import connection
In [3]: from myapp.models import Entry
In [4]: today_entry_list =
Entry.objects.filter(post_date=datetime.date.today())
In [5]: sql, params = today_entry_list.query.sql_with_params()
In [6]: cursor = connection.cursor()
In [7]: cursor.execute('EXPLAIN ' + sql, params)
Out[7]: 1L
In [8]: print(cursor.db.ops.last_executed_query(cursor, sql, params))
EXPLAIN SELECT `myapp_entry`.`id`, `myapp_entry`.`title`,
`myapp_entry`.`slug`, `myapp_entry`.`body`, `myapp_entry`.`post_date` FROM
`myapp_entry` WHERE `myapp_entry`.`post_date` = '2016-04-04'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/17741#comment:4>