【发布时间】:2019-05-31 06:20:20
【问题描述】:
Django 1.11、Python 3.5、Windows 操作系统
我有一个带有 BinaryField 的 Django 模型。当我将模型的实例保存到数据库时,Django 会打印出如下错误消息:
UnicodeEncodeError: 'charmap' codec can't encode character '\ufffd' in position 216: character maps to <undefined>
traceback 的最后一行表明错误发生在 django.db.backends.utils.py 中——我在违规行中添加了注释:
class CursorDebugWrapper(CursorWrapper):
# XXX callproc isn't instrumented at this time.
def execute(self, sql, params=None):
start = time()
try:
return super(CursorDebugWrapper, self).execute(sql, params)
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,
})
##### Error is reported from the logger.debug statement
logger.debug(
'(%.3f) %s; args=%s', duration, sql, params,
extra={'duration': duration, 'sql': sql, 'params': params}
)
所以我认为发生的事情是,当 Django 尝试打印 SQL 插入语句时,它遇到了不可打印的 Unicode 字符并引发错误。我不想在开发时禁用 Django 调试日志记录(当然,它在生产中被禁用)。有什么办法可以解决这个问题?
【问题讨论】:
-
我将
django记录器的处理程序设置为 INFO 并防止错误。但后来我看不到调试级别的日志。 -
您是否尝试过使用
'%r'而不是'%s'来处理可能有问题的项目?