使用SearchQuerySet过滤干草堆结果 [英] Filter haystack result with SearchQuerySet

查看:512
本文介绍了使用SearchQuerySet过滤干草堆结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用haystack with whoosh,尝试将搜索结果限制为仅由当前登录的用户创建的条目。
我创建了一个索引的类别模型有一个外键:

I'm using haystack with whoosh, trying to restrict search results to entries created by the currently logged in user only. The category model, for which I created an index, has a foreign key:

user = models.ForeignKey(User, editable=False)

在我的自定义搜索视图中,我想像这样过滤: p>

And in my custom search view I want to filter like this:

searchqueryset = SearchQuerySet().filter(user=request.user.id)

form = SearchForm(request.GET, searchqueryset=searchqueryset, load_all=True)

    if form.is_valid():
        query = form.cleaned_data['q']
        results = form.search()

在数据库中有一个表类别的条目:

In the database there is one entry for table category:

id  name    user_id 
1   test10  1   

当前的用户ID确实是1.
但是我没有结果。

And the current user id is indeed 1. But I get No results.

当我这样做:

searchqueryset = None

我得到test10类别条目。
所以有人知道为什么用户ID过滤器不按预期工作?

I get the "test10" category entry. So does anyone know why the user id filter isn't working as expected?

推荐答案

你确定您的Haystack索引中的user用户id的整数值?也许它有一些其他用户数据(如用户名),所以比较根本不起作用?

Are you sure that the "user" in your Haystack index holds the integer value of user id? Perhaps it has some other user data(such as username) there, so the comparison simply does not work?

默认情况下,Django用户对象返回用户名,而不是用户ID。

By default the Django user object returns the user name, not the user id.

这篇关于使用SearchQuerySet过滤干草堆结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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