Is there a way to count my queries during a manage.py shell session? It would be helpful as a means to inspect some of the ORM behavior between queries.
Additionally, it would be nice to pull the queries themselves, and I guess their execution times while I’m at it.
i.e.
>>>from myapp.models import User
>>>User.objects.filter(name=’Bob’)
>>>User.objects.filter(name=’Bob’)
>>>[wth is my query count]
>>>User.objects.all()
>>>[wth is my query count]
>>>User.objects.filter(name=’Greg’)
>>>[wth is my query count]
QuerySetobjects have a.count()method, and can also be given tolen()if that’s what you’re looking for:You can also inspect the queries using
._as_sql()or get all the queries already made (with their execution times) with
Note that just calling
User.objects.filter(name='Bob')wont actually exceute any SQL queries. Django waits until you do something with it before it executes the SQL query.You can make the queries more readable by installing the python module
sqlparseand doing something like this:See http://code.google.com/p/python-sqlparse/