django easy audit 记录bytes类型报错

  1. 报错内容:easy audit had a pre-save exception. Traceback (most recent call last): File "D:\python10\lib\site-packages\django\utils\encoding.py", line 70, in force_str s = str(s, encoding, errors) UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\python10\lib\site-packages\easyaudit\signals\model_signals.py", line 90, in pre_save delta = model_delta(old_model, instance) File "D:\python10\lib\site-packages\easyaudit\utils.py", line 56, in model_delta old_value = get_field_value(old_model, field) File "D:\python10\lib\site-packages\easyaudit\utils.py", line 33, in get_field_value value = smart_str(getattr(obj, field.name, None)) File "D:\python10\lib\site-packages\django\utils\encoding.py", line 33, in smart_str return force_str(s, encoding, strings_only, errors) File "D:\python10\lib\site-packages\django\utils\encoding.py", line 74, in force_str raise DjangoUnicodeDecodeError(s, *e.args) django.utils.encoding.DjangoUnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte. You passed in

解决方法:修改D:\python10\lib\site-packages\django\utils\encoding.py的

def force_str(s, encoding="utf-8", strings_only=False, errors="strict"):

    """

    Similar to smart_str(), except that lazy instances are resolved to

    strings, rather than kept as lazy objects.

    If strings_only is True, don't convert (some) non-string-like objects.

    """

    # Handle the common case first for performance reasons.

    if issubclass(type(s), str):

        return s

    if strings_only and is_protected_type(s):

        return s

    try:

        if isinstance(s, bytes):

            s = s.decode(encoding='latin1',errors='strict')

        else:

            s = str(s)

    except UnicodeDecodeError as e:

        raise DjangoUnicodeDecodeError(s, *e.args)

    return s

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值