0
class Article(models.Model):
       title = models.Attribute()
       tags = models.ListField(unicode)

 new = Article(title='what ever')
 new.tags = [ u'Niña', u'Niñb' ]

 new.is_validate()
 >>> True

 new.save()

加载时:</p>

Article.objects.all()

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

在 redis-cli 中:

redis 127.0.0.1:6379> GET "Article:tags:5omv5reh"
(error) ERR Operation against a key holding the wrong kind of value

那么造成这种情况的原因是什么?

4

1 回答 1

1

所以尝试后,unicode字符串应该总是被编码并保存为字符串。当得到时,总是被解码。代码来了:

class Article(models.Model):
       title = models.Attribute()
       tags = models.ListField(str)

 new = Article(title='what ever')
 new.tags = [ u'Niña'.encode('utf-8'), u'Niñb'.encode('utf-8') ]

 new.is_validate()
 >>> True

 new.save()

打印时:

 articles = Article.objects.all()
 for i in articles:
     print i.decode('utf-8')
于 2013-12-06T08:16:51.813 回答