在Django Haystack的MultiValueField中过滤多个值 [英] filtering for multiple values in a MultiValueField in Django Haystack

查看:418
本文介绍了在Django Haystack的MultiValueField中过滤多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个模型,如下所示。权限结构允许Person查看任何具有与它们共享的组的对象,因此如果Person位于组1,2和3中,并且对象与组3,4,5共享,则Person可以通过组3查看。

  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 like SearchQuerySet().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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆