我的mongodb使用utf-8,而django从中读取的数据不是unicode,也许是答案,但如何解决这个问题呢?
mongodb中的数据有一些中文。如果我使用另一个完全由字母或数字组成的表格,它将会很好用。
class Source(Document) : id = IntField() url = StringField() fetchTime = StringField() domain = StringField() xml_tpl_old = StringField() xml_tpl_new = StringField() source = BinaryField() xmlResult = StringField() xmlResult1 = StringField()
如果我使用源表(它有一些中文数据)
Source.objects.all()
django会给出消息
Traceback (most recent call last):
File "E:/Django_project/mysite/mysite/test.py", line 49, in
unicode(Source.objects)
File "D:\Python27\lib\site-packages\mongoengine-0.8.0rc4-py2.7.egg\mongoengine\queryset\queryset.py", line 216, in __repr__
self._populate_cache()
File "D:\Python27\lib\site-packages\mongoengine-0.8.0rc4-py2.7.egg\mongoengine\queryset\queryset.py", line 167, in _populate_cache
self._result_cache.append(self.next())
File "D:\Python27\lib\site-packages\mongoengine-0.8.0rc4-py2.7.egg\mongoengine\queryset\queryset.py", line 1148, in next
raw_doc = self._cursor.next()
File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\cursor.py", line 814, in next
File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\cursor.py", line 763, in _refresh
File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\cursor.py", line 720, in __send_message
File "D:\Python27\lib\site-packages\pymongo-2.5-py2.7-win32.egg\pymongo\helpers.py", line 106, in _unpack_response
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 74: invalid start byte
谢谢!
答案 0 :(得分:-1)
在你的python控制台中试试这个
>>>import sys
>>>sys.getdefaultencoding()
如果结果为' ascii',则应更改默认编码。
答案 1 :(得分:-2)
在将数据保存到数据库之前,我使用base64编码。