Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add SQL comments to a query built with the ORM?

I am trying to identify slow queries in a large-scale Django 1.3 web application. As it is kind of difficult to match the raw sql query in the slow query log with the specific ORM statement in the code, I wondered if it is possible to add a SQL comment to the query constructed with the ORM, something like..

Object.objects.filter(Q(pub_date__lte=datetime.now)).comment('query no. 123')
like image 433
Timo Josten Avatar asked May 30 '13 08:05

Timo Josten


1 Answers

Solution found by using .extra() for raw SQL commands on the django-user mailinglist:

Object.objects.filter(Q(pub_date__lte=datetime.now()).extra(where=['1=1 /* query no. 123 */'])
like image 115
Timo Josten Avatar answered Oct 20 '22 15:10

Timo Josten