【问题标题】:Applying django.db.backends modifications to entire project将 django.db.backends 修改应用于整个项目
【发布时间】:2020-06-25 01:15:43
【问题描述】:

所以我也需要实现自己的 CursorWrappedDebug 来记录错误查询(在文件 django.db.backends.utils.py 中)。 我已经完成了:

logger = logging.getLogger('django.db.backends')

class CustomCursorDebugWrapper(CursorWrapper):
    def execute(self, sql, params=None):
        start = time()
        try:
            return super(CustomCursorDebugWrapper, self).execute(sql, params)

        except Error as e:
            exception=e

        finally:
            stop = time()
            duration = stop - start
            sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            self.db.queries_log.append({
                'sql': sql,
                'time': "%.3f" % duration,
            })
            if 'exception' in locals():
                logger.error('(%.3f) %s; args=%s' % (duration, sql, params),extra={'duration': duration, 'sql': sql, 'params': params})
                raise exception
            else:
                logger.debug('(%.3f) %s; args=%s' % (duration, sql, params),extra={'duration': duration, 'sql': sql, 'params': params})


utils.CursorDebugWrapper = CustomCursorDebugWrapper

现在我需要将这些更改应用到整个项目(所有模块等)而不是当前文件。我应该像自定义数据库后端一样,如果是,那么如何实现自定义数据库后端。

已编辑 找到了解决方案(见链接)https://docs.djangoproject.com/en/3.0/ref/databases/#subclassing-the-built-in-database-backends

【问题讨论】:

  • 我看到您已经找到了解决方案。请将其发布为答案。

标签: python django django-database


【解决方案1】:

找到解决方案(见链接)https://docs.djangoproject.com/en/3.0/ref/databases/#subclassing-the-built-in-database-backends

我的自定义数据库目录:

mydbengine/
        __init__.py
        base.py

文件base.py:

from django.db.backends.postgresql_psycopg2 import base
from django.db.backends.utils import CursorWrapper
from django.db.utils import Error
from time import time
import logging

logger = logging.getLogger('django.db.backends')

class CustomCursorDebugWrapper(CursorWrapper):
    def execute(self, sql, params=None):
        start = time()
        try:
            return super(CustomCursorDebugWrapper, self).execute(sql, params)

        except Error as e:
            exception=e

        finally:
            stop = time()
            duration = stop - start
            sql = self.db.ops.last_executed_query(self.cursor, sql, params)
            self.db.queries_log.append({
                'sql': sql,
                'time': "%.3f" % duration,
            })
            if 'exception' in locals():
                logger.error('(%.3f) %s; args=%s' % (duration, sql, params),extra={'duration': duration, 'sql': sql, 'params': params})
                raise exception
            else:
                logger.debug('(%.3f) %s; args=%s' % (duration, sql, params),extra={'duration': duration, 'sql': sql, 'params': params})


    def executemany(self, sql, param_list):
        start = time()
        try:
            return super(CustomCursorDebugWrapper, self).executemany(sql, param_list)

        except Error as e:
            exception=e

        finally:
            stop = time()
            duration = stop - start
            try:
                times = len(param_list)
            except TypeError:           # param_list could be an iterator
                times = '?'
            self.db.queries_log.append({
                'sql': '%s times: %s' % (times, sql),
                'time': "%.3f" % duration,
            })
            if 'exception' in locals():
                logger.error('(%.3f) %s; args=%s' % (duration, sql, param_list),extra={'duration': duration, 'sql': sql, 'params': param_list})
                raise exception
            else:
                logger.debug('(%.3f) %s; args=%s' % (duration, sql, param_list),extra={'duration': duration, 'sql': sql, 'params': param_list})


class DatabaseWrapper(base.DatabaseWrapper):
    def make_debug_cursor(self, cursor):
        """
        Creates a cursor that logs all queries in self.queries_log.
        """
        return CustomCursorDebugWrapper(cursor, self)

现在我覆盖类 CursorDebugWrapper 以在错误级别记录查询。 (仅限 postgresql)

文件设置.py:

...

DATABASES = {
    'default': {
        'ENGINE': 'database',          
        'NAME': 'name',
        'USER': 'user',
        'PASSWORD': 'password',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

...

应用于项目

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    相关资源
    最近更新 更多