I'm trying to write a JSON initial data fixture that will be loaded after every call to syncdb
.
我正在尝试编写一个JSON初始数据夹具,它将在每次调用syncdb后加载。
I placed an initial_data.json
file in my mysite/myapp/fixtures
directory:
我在我的mysite / myapp / fixtures目录中放置了一个initial_data.json文件:
[
{
"model": "myapp.Person",
"pk": 1,
"fields": {
"first_name": "Tom",
"last_name": "Yam"
}
}
]
Everything is working when the file is encoded in ASCII, but when I save it in UTF-8 encoding (I need to use non-ASCII characters) I get to following error:
当文件以ASCII编码时,一切正常,但当我以UTF-8编码保存时(我需要使用非ASCII字符),我得到以下错误:
Problem installing fixture 'initial_data.json': Traceback (most recent call last):
File "D:\Tom\DjangoEnv\Lib\site-packages\django\core\management\commands\loaddata.py", line 190, in handle
for obj in objects:
File "D:\Tom\DjangoEnv\Lib\site-packages\django\core\serializers\json.py", line 47, in Deserializer
raise DeserializationError(e)
DeserializationError: No JSON object could be decoded
According to the Django documentation, I need to set ensure_ascii=False
when working with non-ASCII data and JSON serializers, but I can't figure how to do it (since its being called from the syncdb
function.
根据Django文档,我需要在使用非ASCII数据和JSON序列化程序时设置ensure_ascii = False,但我无法知道如何操作(因为它是从syncdb函数调用的)。
Any ideas how to use a UTF-8 encoded JASON file as a fixture?
任何想法如何使用UTF-8编码的JASON文件作为夹具?
1 个解决方案
#1
0
load_data
would not pass ensure_ascii
option to serializer so you have two options:
load_data不会将ensure_ascii选项传递给序列化程序,因此您有两个选项:
-
convert data to ascii unicode escaped before loading it, ie:
在加载数据之前将数据转换为ascii unicode,即:
import codecs encoded = codecs.open('/tmp/tst.txt', 'r', 'utf-8').read().encode( 'ascii', 'backslashreplace') open('/tmp/tst-encoded.txt', 'w').write(encoded)
-
write your own management command that would pass
ensure_ascii
编写自己的管理命令,通过ensure_ascii
hope this helps.
希望这可以帮助。
#1
0
load_data
would not pass ensure_ascii
option to serializer so you have two options:
load_data不会将ensure_ascii选项传递给序列化程序,因此您有两个选项:
-
convert data to ascii unicode escaped before loading it, ie:
在加载数据之前将数据转换为ascii unicode,即:
import codecs encoded = codecs.open('/tmp/tst.txt', 'r', 'utf-8').read().encode( 'ascii', 'backslashreplace') open('/tmp/tst-encoded.txt', 'w').write(encoded)
-
write your own management command that would pass
ensure_ascii
编写自己的管理命令,通过ensure_ascii
hope this helps.
希望这可以帮助。