3

I have a Django queryset where I have to use the .extra(select=) method, since I'm calculating a field that requires a WHERE statement subfilter.

I use this WHERE clause in a number of places, and I'd like to be obeying DRY (which is usually somewhat difficult when writing raw SQL). I have written a statement using Django's Q() objects that I could reuse and add into the raw SQL.

I know that you can do Queryset.query.__str__() to get the equivalent SQL output--is there a way to do this with Q objects?

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
  • Where in the documentation does it say `QuerySet.query` gives you the raw SQL? – Games Brainiac Oct 12 '14 at 00:09
  • Sorry, I should have been clearer--`QuerySet.query` as a command doesn't do anything, but if you have a queryset, you can call `.query.__str__()` on it and get the SQL statement back. "`QuerySet`" I was just using as a placeholder. – jdotjdot Oct 12 '14 at 01:09

0 Answers0