简体   繁体   English

当前用户过滤drf-haystack结果

[英]Filtering drf-haystack results by current user

I'm currently using Django 1.10.3, django-haystack search engine with elasticsearch backend, and drf-haystack to prove the views. 我目前正在使用Django 1.10.3,带有Elasticsearch后端的django-haystack搜索引擎以及drf-haystack来证明这些观点。

The searches in general have been great, but I am completely unable to filter results by current user. 总体而言,搜索效果不错,但我完全无法按当前用户筛选结果。

The index is: 索引是:

class SectionIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(document=True, use_template=True, template_name="indexes/structure_text.txt")
    pkey = indexes.IntegerField(model_attr='pk')
    title = indexes.CharField()

for the view (also includes HaystackSerializer, but its generic and not worth including). 用于视图(还包括HaystackSerializer,但它的泛型且不值得包含)。

class SectionSearchView(HaystackViewSet):
    index_models = [Section]
    serializer_class = SectionViewSerializer
    pagination_class = None
    filter_backend = SectionFilter

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def filter_queryset(self, queryset):
        queryset = super(HaystackGenericAPIView, self).filter_queryset(queryset)
        return queryset.using('section')

And lastly, the filter: 最后,过滤器:

class SectionFilter(HaystackFilter):
    mine = django_filters.MethodFilter(action='get_mine')

    class Meta:
        model = Section
        fields = ['mine']

    def get_mine(self, queryset, value):
        try:
            teacher = self.request.user.teacherprofile
            return queryset.filter(supervisors=teacher)
        except:
            return queryset

Section has an M2M field with teacherprofiles, and I basically want to make sure the results ONLY contain the sections where the teacher is in the supervisors. 部门有一个具有教师资料的M2M字段,我基本上想确保结果仅包含教师在主管中的部门。

This implementation returns all matching queries, but ignores the filter condition, without throwing any sort of an error. 此实现返回所有匹配的查询,但忽略过滤条件,而不会引发任何类型的错误。

The "best" result I've gotten was by trying to mess with filter_queryset in the view, adding a .filter(supervisors=teacher) to the queryset, but that returned me ALL the sections with the teacher as supervisor, PLUS all the courses matching the query, regardless of supervisor status or not. 我得到的“最好”结果是试图弄乱视图中的filter_queryset,在查询集中添加一个.filter(supervisors = teacher),但是那让我得到了所有部分,其中有老师作为主管,还有所有课程是否与查询匹配,无论主管状态如何。

So at the end of the day, to whom it may concern, I eventually used a SearchQuerySet to return results for the classes, then convert the results to a list and removing the items without the teacher as a supervisor. 因此,最终,我可能会担心的是,最终我使用了SearchQuerySet返回课程的结果,然后将结果转换为列表,并在没有老师作为主管的情况下删除了项目。

It's probably not the most efficient way, and I wasnt able to get it working within Haystack alone (even using SQS with the filter_and(name, supervisor) conditions), but it works, and still performs more than well enough performance wise. 这可能不是最有效的方法,而且我无法单独在Haystack上运行它(即使在filter_and(name,supervisor)条件下使用SQS),但它仍然有效,并且在性能方面仍然表现出色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM