Задать вопрос

Тел: +7 965 3737 888

495

Просмотров

2

Ответов

Pretty print SQL of query sets

<p>Install <a href="http://code.google.com/p/python-sqlparse/">sqlparse</a> with easy_install sqlparse and then you can easily debug the SQL like this:</p>
def view(request):
    data = MyModel.objects.filter(something__very=complex)
    print_sql(data)
    ...

<p>Inspired by <a href="http://simonwillison.net/2009/Apr/28/">Simon Willison</a></p>

Вопрос полезен? Да0/Нет0
file_2616.py(170.0б)
None

Ответы (2):

Ответcanassa:06.08.2010
Ответ полезен? Да0/Нет0

This should work in Django 1.2:

def print_sql(qs):
    q = qs.query.__str__()
    print sqlparse.format(q, reindent=True, keyword_case='upper')

Ответpeterbe:05.05.2009
Ответ полезен? Да0/Нет0

You might want to wrap this with something so that this doesn't get used in non-development mode. For example:

if not settings.DEBUG:
    def print_sql(*__):
        pass