【发布时间】:2021-05-27 13:57:38
【问题描述】:
我有一个 Django 2.2.23 应用程序,在 Python 3.9.4 上运行。我有 django 扩展 2.2.9。
我有一个具有django_extensions.db.fields.json.JSONField 属性的模型(AFAIK 只是一个自动序列化的文本字段)。我提到这一点是因为当 JSON 被反序列化时,django-extensions 库会这样:
def loads(txt):
value = json.loads(
txt,
encoding=settings.DEFAULT_CHARSET
)
return value
问题是import json 导入的库在这样调用时会报错:
Python 3.9.4 (default, Apr 5 2021, 01:50:46)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> json.loads("{}", encoding="UTF-8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 359, in loads
return cls(**kw).decode(s)
TypeError: __init__() got an unexpected keyword argument 'encoding'
>>>
最终结果是我无法从数据库中加载任何包含 JSONField 的记录,因为 JSONDecoder 无法处理传递 encoding 参数的问题。
我的印象是 Python 的 json 库是 simplejson。但是看看源代码,它确实处理了encoding。但是,如果我查看“/usr/local/Cellar/python@3.9/3.9.4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/init”中的 json 库>.py"(如上面错误中指定的),这个解码器肯定没有。
这显然是不正确的行为,但我不知道我需要升级什么才能让正确的事情发生。
【问题讨论】: