18π
If the underlying database is PostgreSQL you can do:
from django.db import connection
sql, params = queryset.query.sql_with_params()
cursor = connection.cursor()
cursor.mogrify(sql, params)
sql_with_params
returns the plain query without any values substituted and the parameters that will be inserted in the query.
It is still not recommended to use .mogrify()
for other purposes than debugging because the method may disappear in the future.
If you want to execute the query, you can/should just use .raw()
.
YourModel.objects.raw(sql, params)
π€kev
4π
not quite what you want, but if you have DEBUG = True
you can use
from django.db import connection
connection.queries
update:
looking at the Queryset
__str__
method:
__str__(self)
| Returns the query as a string of SQL with the parameter values
| substituted in.
|
| Parameter values won't necessarily be quoted correctly, since that is
| done by the database interface at execution time.
π€second
- How do I schedule a task with Celery that runs on 1st of every month?
- Django queryset: Exclude list of emails using endswith
- Display foreign key columns as link to detail object in Django admin
2π
If this is for debug purpose you should look into django-debug-toolbar that will show you all queries ran for any view youβre looking at
π€krs
- Invalid HTTP_HOST header: The domain name provided is not valid β requests to dockerized django app using container name
- How to return data with 403 error in Django Rest Framework?
Source:stackexchange.com