Printing Django QuerySet SQL with “”

后端 未结 3 779
难免孤独
难免孤独 2021-01-18 01:41

You can print a queryset\'s SQL as follows:

print str(queryset.query)

however, for some reason this removes quotation marks, so you get:

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-18 02:28

    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.
    

提交回复
热议问题