我正在使用 loaddata 重新加载夹具(由于某种原因,数据库中的数据似乎已损坏)。这样做会导致错误,表明loaddata
将 xml 视为 ascii。实际上是 UTF-8 导致了错误:
$ python manage.py loaddata --traceback ./countries/fixtures/initial_data.xml
Traceback (most recent call last):
File "/home/marcintustin/oneclickrep/oneclickcosvirt/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 202, in handle
'error_msg': e
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf1 in position 6: ordinal not in range(128)
(增加详细程度只是告诉我它在哪里寻找固定装置。没有更多信息。这是由 提供的完整堆栈跟踪loaddata
)。
夹具开始:
<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
<object pk="AF" model="countries.country">
<field type="CharField" name="name">AFGHANISTAN</field>
<field type="CharField" name="printable_name">Afghanistan</field>
<field type="CharField" name="iso3">AFG</field>
<field type="PositiveSmallIntegerField" name="numcode">4</field>
</object>
<!-- rest of file -->
</django-objects>
它声明自己是 utf-8,它是 utf-8(我使用 emacs 将其重新保存为 utf-8,只是为了确定)。
这里的问题不是我的输入是 utf-8,而是loaddata
想把它当作别的东西。我怎样才能强制loaddata
解码它?或者loaddata
,在我手动将我的 xml 加载到 unicode 字符串之后,以编程方式调用这些东西有多容易?
在 linux 上使用 django 1.4,使用 sqlite 作为数据库后端。