[英]Django haystack, How to search for a ManyToMany related field?
I've added a MultivaluedField
to my index (haystack), I need to search for a ManyToMany related field, but it doesn't work.我已将
MultivaluedField
添加到我的索引 (haystack) 中,我需要搜索 ManyToMany 相关字段,但它不起作用。
The engine is WHOOSH.引擎是WHOOSH。
This how my index looks like:这是我的索引的样子:
class PostIndex(SearchIndex):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='author')
body = CharField(model_attr='body')
pub_date = DateTimeField(model_attr='publish')
regions = MultiValueField()
def prepare_regions(self, obj):
return [region.name for region in obj.regions.all()]
And this how my model looks like:这就是我的模型的样子:
class Post(models.Model):
title = models.CharField(_('title'), max_length=200)
author = models.ForeignKey(User, blank=True, null=True)
body = models.TextField(_('body'), )
allow_comments = models.BooleanField(_('allow comments'), default=True)
publish = models.DateTimeField(_('publish'), default=datetime.datetime.now)
categories = models.ManyToManyField(Category, blank=True)
tags = TagField()
objects = PublicManager()
regions = models.ManyToManyField(Region, blank=True)
If I use SearchQuerySet().filter(region__in=words_list)
it works.如果我使用
SearchQuerySet().filter(region__in=words_list)
它可以工作。 The problem is that I don't know when the user is searching for a region or another field, so I have to use SearchQuerySet().filter(content__icontains=words_list)
.问题是我不知道用户何时搜索区域或其他字段,所以我必须使用
SearchQuerySet().filter(content__icontains=words_list)
。 And in this way nothing is found.通过这种方式,什么也找不到。
Thanks谢谢
Thanks!!谢谢!!
You only add region id to the index for Region.您只需将区域 ID 添加到区域的索引中。
Try尝试
def prepare_regions(self, obj):
return [region.pk for region in obj.regions.all()]
Try :尝试 :
class PostIndex(SearchIndex):
text = CharField(document=True, use_template=True)
author = CharField(model_attr='author')
body = CharField(model_attr='body')
pub_date = DateTimeField(model_attr='publish')
regions = CharField(model_attr='regions')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.