Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django 1.2 Equivalent of QuerySet.query.as_sql()

In Django 1.1 I was able to produce the SQL used by a QuerySet with this notation:

QuerySet.query.as_sql()

In Django 1.2, this raises as AttributeError.

Anyone know the Django 1.2 equivalent of that method?

Thanks

like image 698
Zach Avatar asked May 24 '10 20:05

Zach


People also ask

How do I get QuerySet in Django?

You get a QuerySet by using your model's Manager . Each model has at least one Manager , and it's called objects by default. Access it directly via the model class, like so: >>> Blog.objects <django.db.models.manager.Manager object at ...> >>> b = Blog(name='Foo', tagline='Bar') >>> b.objects Traceback: ...

What is the type of QuerySet in Django?

A QuerySet is a collection of data from a database. A QuerySet is built up as a list of objects. QuerySets makes it easier to get the data you actually need, by allowing you to filter and order the data.

Does Django ORM support subquery?

Django allows using SQL subqueries.

What is the command to generate the SQL equivalent to Django model?

The raw() manager method can be used to perform raw SQL queries that return model instances: Manager. raw (raw_query, params=(), translations=None) This method takes a raw SQL query, executes it, and returns a django.


2 Answers

In Django 1.1, QuerySet.query returned a BaseQuery object, now it returns a Query objects. The query object has a __str__ method defined that returns the SQL.

like image 195
Zach Avatar answered Nov 06 '22 12:11

Zach


as answered in In django 1.2.1 how can I get something like the old .as_sql?

it's just:

print QuerySet.query
like image 42
fastmultiplication Avatar answered Nov 06 '22 11:11

fastmultiplication