在Django Haystack的MultiValueField中过滤多个值 [英] filtering for multiple values in a MultiValueField in Django Haystack
问题描述
class Person(models.Model):
groups = models.ManyToManyField(Group)
class Object(models.Model):
groups = models.ManyToManyField(Group)
SearchIndex是这样的:
class ObjectIndex(indexes.SearchIndex,indexes.Indexable):
groups = indexes.MultiValueField(null = True)
def prepare_groups(self,obj):
return [group.pk for obj.groups.all()]或没有
那么,创建一个 SearchQuerySet
允许我采取类似 SearchQuerySet()。models(Object).filter(groups = aperson.groups.all())
这是一个OR组而不是AND? / p>
看起来正确的方法是:
SearchQuerySet()。models(Object).filter(groups__in = [g.id for g in aperson.groups.all()]
/ pre>
I've got two models like below. The permission structure allows a Person to see any object that has a Group in common with them, so that if a Person is in Groups 1, 2, and 3, and an Object is shared with Groups 3, 4, 5, the Person can see it through Group 3.
class Person(models.Model): groups = models.ManyToManyField(Group) class Object(models.Model): groups = models.ManyToManyField(Group)
The SearchIndex is like this:
class ObjectIndex(indexes.SearchIndex, indexes.Indexable): groups = indexes.MultiValueField(null=True) def prepare_groups(self, obj): return [group.pk for group in obj.groups.all()] or None
So, what is the best way to create a
SearchQuerySet
that allows me to take something likeSearchQuerySet().models(Object).filter(groups=aperson.groups.all())
that is an OR on the groups instead of an AND?解决方案It looks like the correct way to do this is:
SearchQuerySet().models(Object).filter(groups__in=[g.id for g in aperson.groups.all()])
这篇关于在Django Haystack的MultiValueField中过滤多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!