Django: show/log ORM sql calls from python shell

后端 未结 7 1172
臣服心动
臣服心动 2020-11-30 16:33

Using the excellent Django-Devserver I\'m finding all kinds of interesting and unexpected SQL calls in my code. I wanted to find where the calls are coming from, and so I\'m

7条回答
  •  孤街浪徒
    2020-11-30 17:21

    I was trying to use "Django: show/log ORM sql calls from python shell" in a shell on a production server, and it wasn't working. Eventually someone pointed out that it will only do this debug logging when DEBUG = True. But you can work around that like this:

    import logging
    from django.db import connection
    connection.force_debug_cursor = True  # Change to use_debug_cursor in django < 1.8
    l = logging.getLogger('django.db.backends')
    l.setLevel(logging.DEBUG)
    l.addHandler(logging.StreamHandler())
    

    I'm leaving this here so I can find it later, and hopefully it saves someone else the same digging I did.

提交回复
热议问题