Redisco ListField unicode save error but validate true

143 views Asked by At
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()

While when load:

Article.objects.all()

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

In redis-cli:

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

So what's the reason that caused this?

1

There are 1 answers

0
Scen On

So after trying, the unicode string should always be encode and saved as string. When get, always be decode. here comes the code:

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()

When print:

 articles = Article.objects.all()
 for i in articles:
     print i.decode('utf-8')