- 报错内容: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