<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Django foo &#187; query</title>
	<atom:link href="http://www.djangofoo.com/tag/query/feed" rel="self" type="application/rss+xml" />
	<link>http://www.djangofoo.com</link>
	<description>Django Tips and Tweaks</description>
	<lastBuildDate>Wed, 18 Aug 2010 09:38:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Show SQL queries</title>
		<link>http://www.djangofoo.com/103/show-sql-queries</link>
		<comments>http://www.djangofoo.com/103/show-sql-queries#comments</comments>
		<pubDate>Sat, 27 Feb 2010 00:29:48 +0000</pubDate>
		<dc:creator>Davo</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[db.connection]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[show queries]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://www.djangofoo.com/?p=103</guid>
		<description><![CDATA[The Django ORM is just awesome but for debugging purposes it is usefull to be able to see what queries are actually made to the database. It is possible to have a history of all the queries made until the request is finished. Additionaly the query object can be accessed to retrieve the SQL statement. [...]]]></description>
			<content:encoded><![CDATA[<p>The Django ORM is just awesome but for <strong>debugging</strong> purposes it is usefull to be able to see what <strong>queries</strong> are actually made to the database. It is possible to have a history of all the queries made until the request is finished. Additionaly the <strong>query</strong> object can be accessed to retrieve the SQL statement.</p>
<p>Importing the connection and printing the queries</p>
<pre class="brush: python">
>>> from django.db import connection

>>> print connection.queries
[]
>>>  # as you can see it prints an empty list since no queries are made yet!
</pre>
<p>Make a call to the database and print again the queries</p>
<pre class="brush: python">
# I am using a model from my app. This is just an example.
>>> from apps.cobblers.models import Cobbler

>>> Cobbler.objects.filter()
[&lt;Cobbler: 99 Dry Cleaners&gt;]

>>> print connection.queries
[{'time': '0.000', 'sql': u'SELECT `cobblers_cobbler`.`id`, `cobblers_cobbler`.`name`, `cobblers_cobbler`.`description`, `cobblers_cobbler`.`address`, `cobblers_cobbler`.`city`, `cobblers_cobbler`.`post_code`, `cobblers_cobbler`.`telephone`, `cobblers_cobbler`.`email` FROM `cobblers_cobbler` ORDER BY `cobblers_cobbler`.`name` ASC LIMIT 21'}]
# the result is different, we have the latest query.
</pre>
<p>Finally it is also possible to see the query which will be executed for a given statement</p>
<pre class="brush: python">
>>> Cobbler.objects.filter(id=1).query
&lt;django.db.models.sql.query.Query object at 0x8940c2c&gt;

>>> str(Cobbler.objects.filter(id=1).query)
'SELECT `cobblers_cobbler`.`id`, `cobblers_cobbler`.`name`, `cobblers_cobbler`.`description`, `cobblers_cobbler`.`address`, `cobblers_cobbler`.`city`, `cobblers_cobbler`.`post_code`, `cobblers_cobbler`.`telephone`, `cobblers_cobbler`.`email ` FROM `cobblers_cobbler` WHERE `cobblers_cobbler`.`id` = 1  ORDER BY `cobblers_cobbler`.`name` ASC'
>>>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.djangofoo.com/103/show-sql-queries/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

