【发布时间】:2016-11-23 10:33:15
【问题描述】:
我有博客文章,我正在尝试将 cmets 添加到搜索索引,但由于某种原因它不起作用... 如果重要,我的 cmets 具有通用外键。 顺便说一句,它适用于标签。
class BlogIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
headline = indexes.CharField(model_attr="headline", null=True)
body = indexes.CharField(model_attr="body")
date = indexes.DateTimeField(model_attr='date')
mytags = indexes.MultiValueField()
mycomments = indexes.MultiValueField()
def prepare_mytags(self, obj):
return [tag.name for tag in obj.tags.all()]
def prepare_mycomments(self, obj):
cur_model = ContentType.objects.get_for_model(obj)
cur_comments = Comment.objects.filter(content_type=cur_model, object_pk=obj.id)
return [str(cur_c.content) for cur_c in cur_comments]
def get_model(self):
return Snip
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.filter(date__lte=timezone.now())
这是我的模板:
{{ object.headline }}
{{ object.body }}
{% for c in object.mycomments %}{{ c }} {% endfor %}
{% for tag in object.tags.all %}{{ tag.name }} {% endfor %}
如果重要的话,我也在使用 drf-haystack,这是序列化程序:
class BlogSearchSerializer(HaystackSerializer):
class Meta:
index_classes = [BlogIndex]
fields = [
"headline", "body", "date", "mytags", 'mycomments', "content",
]
当我使用 content=XX 进行搜索时,它在 cmets 中找不到任何内容。
我做错了什么?
谢谢 回复
【问题讨论】:
标签: django indexing full-text-search django-rest-framework django-haystack