Re: Possible to see sql for queryset before database gets it?
Thanks for those tips. Great help. On Jan 19, 12:37 am, Malcolm Tredinnickwrote: > On Sun, 2009-01-18 at 17:46 -0200, Ramiro Morales wrote: > > [] > > > > > or if you are using a recent trunk version (more recent than two weeks or > > so) > > you might want to try printing the output of: > > > .as_sql() > > Please don't recommend that one, it's very likely to change in the near > future (like, this week some time), as I'm becoming unhappy with the > name choice for the reasons given below. > > I was trying to keep the internal code clean by not having to if-case > yet another method name.. The problem with the current method name is > that it leaks the "SQL" concept into QuerySets, which should, ideally, > be agnostic about what the storage method is. Unnecessarily leaky > abstraction and it's keeping me awake at night. > > Every QuerySet that has some kind of backing store should have a "query" > attribute and, if it supports SQL, will have an as_sql() method. (The > previous sentence is written in a very future-proof fashion. It's > trivially true for Django at the moment.) The queryset.query.as_sql() > method is the best way to look at the generated SQL. > > Regards, > Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Possible to see sql for queryset before database gets it?
On Sun, 2009-01-18 at 17:46 -0200, Ramiro Morales wrote: [] > > or if you are using a recent trunk version (more recent than two weeks or so) > you might want to try printing the output of: > > .as_sql() Please don't recommend that one, it's very likely to change in the near future (like, this week some time), as I'm becoming unhappy with the name choice for the reasons given below. I was trying to keep the internal code clean by not having to if-case yet another method name.. The problem with the current method name is that it leaks the "SQL" concept into QuerySets, which should, ideally, be agnostic about what the storage method is. Unnecessarily leaky abstraction and it's keeping me awake at night. Every QuerySet that has some kind of backing store should have a "query" attribute and, if it supports SQL, will have an as_sql() method. (The previous sentence is written in a very future-proof fashion. It's trivially true for Django at the moment.) The queryset.query.as_sql() method is the best way to look at the generated SQL. Regards, Malcolm --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Possible to see sql for queryset before database gets it?
On Sun, Jan 18, 2009 at 5:27 PM, phoebebrightwrote: > > I have a problem with the sql being generated by this: > >tasks = Task.objects.filter(status='open').order_by('-priority') >tasks = tasks.extra(where=['list IN %s'], params= > [settings.MY_LISTS]) > > Is there a command where I can say something like 'print tasks.sql' > that would generate the sql but not send it to the database, so I can > see exactly why the sql is failing? First, you can try what's suggested at this Django FAQ entry: http://docs.djangoproject.com/en/dev/faq/models/#how-can-i-see-the-raw-sql-queries-django-is-running For another option, try printing the output of: .query.as_sql() or if you are using a recent trunk version (more recent than two weeks or so) you might want to try printing the output of: .as_sql() They should print the complete SQL query generated by the QuerySet. Both the query attribute and as_sql() method are internal implementation details of QuerySet. That's why they are not documented, but you can count on them being present for the foreseeable future. HTH -- Ramiro Morales --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---