Using JTF file with UTF-8 encoding in Django

I am trying to write a source JSON device that will load after every syncdb call.

I put the initial_data.json file in my mysite/myapp/fixtures :

 [ { "model": "myapp.Person", "pk": 1, "fields": { "first_name": "Tom", "last_name": "Yam" } } ] 

Everything works when the file is encoded in ASCII, but when I save it in UTF-8 encoding (I need to use characters other than ASCII), I get the following error:

 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 how to do this (since it is called from the syncdb function.

Any ideas on how to use a UTF-8 encoded JASON file as a tool?

+4
source share
1 answer

load_data will not pass the ensure_ascii parameter to the serializer, so you have two options:

  • convert data to ascii unicode that were escaped before loading, i.e.:

     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 team that will pass ensure_ascii

hope this helps.

0
source

Source: https://habr.com/ru/post/1436732/


All Articles