Can Django ORM output the SQL query it uses?

I know that you can output SQL to see the created tables. Is it possible for Django to output sql used for any query, for example:

Protocols.objects.filter(active=False) 

? I could not find this in the docs, so hopefully someone can point them to me if Django can actually do this.

+4
source share
3 answers

See the Django FAQ: How can I view raw Django SQL queries? :

 >>> from django.db import connection >>> connection.queries = [] >>> Protocols.objects.filter(active=False) >>> print connection.queries 
+10
source

Yes maybe. You must make sure that DEBUG = True
in your settings file

you can see which sql queries were executed ...

 >>> from django.db import connection >>> connection.queries 

Obviously, you need to complete some queries to see them here.

To find out what is done to deliver a specific QuerySet, you can do the following

str (MyModel.objects.filter (myvar__gte = 15) .query)

Documented here is http://docs.djangoproject.com/en/dev/faq/models/

+8
source

I found that the Django debug toolbar is priceless. There's also .as_sql () for the intra-code display of things (see this SO post for a note that there is a nice line in it)

+3
source

Source: https://habr.com/ru/post/1336509/


All Articles