turn off SQL logging while keeping settings.DEBUG?

前端 未结 4 699
生来不讨喜
生来不讨喜 2020-12-14 01:31

Django logs SQL operations to an internal buffer (whether logging to file or not) when settings.DEBUG=True. Because I have long-running process that does a lot of DB operat

4条回答
  •  再見小時候
    2020-12-14 01:56

    When settings.DEBUG is True, Django uses CursorDebugWrapper instead of CursorWrapper. This is what appends the queries to connection.queries and consumes memory. I would monkey-patch the connection wrapper to always use CursorWrapper:

    from django.conf import settings
    from django.db.backends import BaseDatabaseWrapper
    from django.db.backends.util import CursorWrapper
    
    if settings.DEBUG:
        BaseDatabaseWrapper.make_debug_cursor = lambda self, cursor: CursorWrapper(cursor, self)
    

    Disabling logging like others suggest won't fix the problem, because CursorDebugWrapper still stores the queries in connection.queries even if logging is off.

提交回复
热议问题