重建索引时的干草堆UnicodeDecodeError

时间:2014-11-26 16:31:21

标签: python solr django-haystack

我在一个小项目中使用了Haystack和Solr,我遇到了一个问题。 问题似乎与Unicode字符有关,我有一个名为Channel的模型,其中一个字段是描述。在这个领域碰巧存在非ascii chars。

class Channel(models.Model):
    owner = models.ForeignKey(User, null=True, related_name='channels')
    name = models.CharField(max_length=20, db_index=True, unique=True)
    image = models.URLField()
    description = models.CharField(max_length=255)
    kind = models.IntegerField(default=1, choices=CHANNEL_CHOICES)
    hidden = models.BooleanField(default=False)

    subscriptions = models.IntegerField(default=0)

    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

我按照以下方式制作了一个SearchIndex

class ChannelIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    name = indexes.NgramField(model_attr='name')
    id = indexes.IntegerField(model_attr='id')
    description = indexes.CharField(model_attr='description')
    image = indexes.CharField(model_attr='image')
    hidden = indexes.BooleanField(model_attr='hidden')
    kind = indexes.IntegerField(model_attr='kind')

    def get_model(self):
        return Channel 

现在,当在描述字段中(但我认为其他人也会发生这种情况)有非ascii字符,我在尝试重建索引时遇到错误。

  File "/usr/lib/python2.7/httplib.py", line 962, in request
self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 996, in _send_request
self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 958, in endheaders
self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 816, in _send_output
msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5173: ordinal not in range(128)

当我创建python manage.py rebuild_index

时会显示此错误

0 个答案:

没有答案