tags:

views:

109

answers:

1

In past versions of django you could construct a queryset and then do .as_sql() on it to find out the final query.

in Django 1.2.1 there is a function ._as_sql() which returns something similar, but not the same.

In past versions:

qs=Model.objects.all()
qs.as_sql() ====>

SELECT `model_table.id`, `model_table.name`, `model_table.size` from model_table

This shows me a lot of information.

But if I try it in Django 1.2.1

from django.db import connections
con=connections['default']

qs=Model.objects.all()
qs._as_sql(con) ====>

SELECT U0.`id` from model_table U0

This doesn't show me what fields are actually being selected. I know this information is available somewhere, because in templates, I can still do:

{% for q in sql_queries %}
    {{q.time}} - {{q.sql}}
{% endfor %}

which shows me the full version of the query (including the fields selected)

My question is, how can I get this full version within the shell?

+1  A: 
qs=Model.objects.all()
qs.query.as_sql() 

Should do the job as it is shown here

EDIT:

I just try it and get the same error.

qs=Model.objects.all()
print qs.query

this must give you what you want (:

FallenAngel
what version of django are you in?in 1.2.1, your code generates 'Query' object has no attribute 'as_sql'
fastmultiplication
sorry for untested code. check my edit.
FallenAngel
if this solution fits your needs, please mark it as answered
FallenAngel
It's `str(qs.query)` to be precise. See http://stackoverflow.com/questions/2900057/django-1-2-equivalent-of-queryset-query-as-sql
Tobu
Thanks for your advice...
FallenAngel